This plugin intercepts 5xx server error responses and replaces them with a 302 redirect to a custom error page. It preserves the original status code in a custom header (Origin-Status) for debugging purposes. Use this plugin when you need to provide a user-friendly error page for server errors, improve user experience during outages, or centralize error handling across multiple backend services. It operates during the response headers processing phase.
- The proxy receives an HTTP response from the upstream server and invokes the plugin's
on_http_response_headerscallback. - The plugin reads the
:statuspseudo-header and parses it as an integer. - If the status code is in the 5xx range (500-599), the plugin calls
send_http_responseto generate a new 302 (Found) redirect response. - The redirect response includes:
Locationheader pointing to a custom error page (https://storage.googleapis.com/www.example.com/server-error.html)Origin-Statusheader containing the original 5xx status code for logging/debugging
- The plugin returns
Action::Continue, and the proxy sends the redirect response to the client instead of the original 5xx error.
- Status parsing: The plugin extracts the
:statuspseudo-header; C++ usesabsl::SimpleAtoi(). - Redirect generation: A 302 redirect is sent directly from the plugin instead of returning the upstream response. C++ requires returning
FilterHeadersStatus::ContinueAndEndStreamafter sending the local response to halt the original response body. - Diagnostic headers: The original status code is preserved in the custom
Origin-Statusheader. - Redirection URL: The destination error page is hardcoded as a constant string.
No configuration required. The redirect URL is hardcoded as a constant:
- C++:
redirect_page = "https://storage.googleapis.com/www.example.com/server-error.html" - Go:
redirectPage = "https://storage.googleapis.com/www.example.com/server-error.html" - Rust:
REDIRECT_PAGE = "https://storage.googleapis.com/www.example.com/server-error.html"
Build the plugin for any supported language from the plugins/ directory:
# Rust
bazelisk build //samples/add_custom_response:plugin_rust.wasm
# C++
bazelisk build //samples/add_custom_response:plugin_cpp.wasm
# Go
bazelisk build //samples/add_custom_response:plugin_go.wasmRun the unit tests defined in tests.textpb:
# Using Docker (recommended)
docker run -it -v $(pwd):/mnt \
us-docker.pkg.dev/service-extensions-samples/plugins/wasm-tester:main \
--proto /mnt/samples/add_custom_response/tests.textpb \
--plugin /mnt/bazel-bin/samples/add_custom_response/plugin_rust.wasm
# Using Bazel (all languages)
bazelisk test --test_output=all //samples/add_custom_response:testsDerived from tests.textpb:
| Scenario | Description |
|---|---|
| NoRedirect | Does nothing for successful responses. |
| DoRedirect | Intercepts a 5xx response and redirects the client to the custom error page while preserving the original status code. |