Proxy error responses
The proxy classifies request, service, and upstream failures before it sends a client-facing response. A shared error middleware then turns the selected status into an embedded or service-specific HTML page while preserving normal upstream responses.
Error response flow
Error classification
The proxy records an error response during request handling instead of writing it at the first failure point. This gives the outer middleware a chance to select the right page and lets nested service pages take priority over the proxy-wide defaults.
When a request moves through the proxy, the response path is:
flowchart LR
A[Client request] --> B[Router and service]
B --> C[Request checks]
C --> D[Target and upstream]
D --> E{Failure?}
E -- No --> F[Normal upstream response]
E -- Yes --> G[Classify status]
G --> H[Set error response]
H --> I[Service page or embedded page]
I --> J[Client response]
SetErrorResponse stores the status and optional template data in the request context. If no error-page middleware is present, it uses the standard plain-text HTTP error instead. A client that disconnects is a special case: the proxy records status 499 for logs but does not try to send a page to a client that is no longer connected.
References: internal/…/error_page_middleware.go• L12-41 internal/…/target.go• L361-399
Error page rendering
ErrorPageMiddleware renders a page after the wrapped handler returns. It sets an HTML content type, writes the selected status, and uses the matching <status>.html template when one exists. A missing or failed template falls back to a simple status heading at the root middleware.
The server-wide middleware uses the embedded default pages. A service can add its own page directory inside that middleware chain. The service page handles a status when it has a matching template; otherwise the outer default page handles it.
References: internal/…/error_page_middleware.go• L43-103 internal/…/server.go• L218-229 internal/…/service.go• L586-609
Failure classification
Request failures
Request failures happen before the upstream can produce a useful response. The proxy maps body limits to 413, body read timeouts to 408, and malformed chunked input to 400. An unexpected request-buffer failure becomes 500.
The same request checks also apply when the target proxy reads the request body. This keeps the client-visible result consistent whether request buffering is enabled at the outer request path or fails during forwarding.
References: internal/…/request_buffer_middleware.go• L23-47 internal/…/target.go• L361-404 internal/…/errors.go• L3-21
Upstream failures
Upstream failures occur while the proxy sends a request or reads the upstream result. A timeout becomes 504, while a general proxy error such as an invalid upstream response becomes 502. The proxy also maps a request that is cancelled during target draining to 504, because the request cannot complete through the retiring target.
An ordinary HTTP response from the upstream is not reclassified. The error handler is used for transport and proxy errors, so valid upstream status codes and bodies continue to the client.
References: internal/…/target.go• L293-304 internal/…/target.go• L361-419
Service availability failures
Service availability failures happen before or instead of target forwarding. The router returns 404 when no service matches the request. The load balancer returns 503 when it cannot claim a target, and a stopped service also returns 503 with an optional message. A pause that exceeds its wait limit returns 504.
The proxy also returns 503 when a request reaches a service over TLS even though that service does not have TLS enabled. These results describe proxy state rather than an upstream response.
References: internal/…/router.go• L136-149 internal/…/load_balancer.go• L174-189 internal/…/service.go• L612-663
Response size failures
Response buffering protects the proxy from sending a response larger than the configured limit. If the buffer exceeds that limit or cannot send its stored data, the middleware selects 500; streaming responses bypass this buffering path.
This failure is distinct from an upstream 502: the upstream may have returned a valid response, but the proxy cannot safely deliver it under its response policy.
References: internal/…/response_buffer_middleware.go• L18-44 internal/…/response_buffer_middleware.go• L55-104 internal/…/target.go• L121-128
Client-facing pages
Embedded default pages
The embedded page set provides the proxy-wide fallback for common error statuses. The pages are static HTML and do not expose upstream response details. The 503 page can also display a service-supplied message when the service is stopped with one.
| Status | Default page purpose |
|---|---|
404 | No service matches the request |
413 | Request content is too large |
502 | The upstream returned an invalid or unusable result |
503 | The service is unavailable |
504 | The upstream or proxy wait exceeded its limit |
Statuses without an embedded page use the root middleware's simple status heading. A response that already came from the upstream keeps its own body and content type.
References: internal/…/embed.go• L1-6 internal/…/404.html• L7-11 internal/…/413.html• L7-11 internal/…/502.html• L7-11 internal/…/503.html• L7-11 internal/…/504.html• L7-11
Service-specific pages
A service may point to a directory of HTML pages through its error-page setting. The proxy loads matching status files for that service and places this handler inside the service request path, so a service page can replace the shared page without changing error classification.
The directory must contain parseable HTML templates. A bad directory or invalid template prevents the service middleware from being created. A valid directory that lacks a status file does not hide the outer default page.
References: internal/…/service.go• L101-118 internal/…/service.go• L586-609 internal/…/error_page_middleware.go• L36-47 internal/…/error_page_middleware_test.go• L71-117
Related systems
Error responses sit at the end of several proxy flows. Read proxy server for the request lifecycle, request routing for service selection, service and target lifecycle for pause and drain behavior, load balancing for target availability, and TLS and certificates for TLS-related service failures.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the status classification in
Target.handleProxyErroraligned with request-buffer and service-state failures. - Keep every embedded status page named with the exact status code that the middleware looks up.
- When changing nested middleware order, preserve the rule that service pages can override matching proxy-wide pages and that the root handler can still render a fallback.
- When adding template data to a new status, confirm that both service-specific and embedded-page paths handle the data shape.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Error-page templates cannot be parsed at startup or service creation | The affected handler cannot be built | Validate the page directory and template syntax before enabling the service |
| A status has no matching page in an inner middleware | The outer page set gets the next chance to render it | Keep the root default middleware around the router |
| A response exceeds the configured buffer limit | The client receives 500 instead of the upstream body | Set a response limit that matches the service contract or disable response buffering for that target |
| A target is draining when the load balancer claims it | The request receives 503 and no target request starts | Wait for target health and lifecycle state to settle before routing traffic |
| The client cancels the request | No client-facing error page is sent; the proxy records 499 | Treat the status as a disconnect signal, not as an upstream outage |
Key files
| File | Why to read it |
|---|---|
| internal/…/error_page_middleware.go | Defines deferred error selection, template lookup, nesting, and fallback output. |
| internal/…/target.go | Defines upstream error classification and target proxy integration. |
| internal/…/service.go | Connects service state and custom pages to client responses. |
| internal/…/request_buffer_middleware.go | Shows request-size, timeout, and malformed-input responses. |
| internal/…/response_buffer_middleware.go | Shows response-size protection and its 500 result. |
| internal/…/server.go | Shows the proxy-wide placement of the default error middleware. |
| internal/…/embed.go | Defines the embedded default page set. |
| internal/…/404.html, internal/…/413.html, internal/…/502.html, internal/…/503.html, internal/…/504.html | Show the client-facing default messages for the main proxy error classes. |