feat(http): allow error hook to override 502 responses by fkesheh · Pull Request #7999 · mitmproxy/mitmproxy
When a ResponseProtocolError occurs (e.g., upstream connection failure), addons can now set flow.response in the error hook to serve a custom response instead of the default 502 error. This enables common use cases like: - Serving cached responses when upstream is unavailable - Implementing circuit breaker patterns with fallback responses - Providing custom error pages instead of generic 502 errors - Graceful degradation during network failures Implementation details: - Tracks whether response was set before or during the error hook - Only applies override when response is newly set in the hook - Sets proper timestamps on the override response - Uses raw_content for consistency with send_response() - Handles trailers correctly - Works across HTTP/1.1, HTTP/2, and HTTP/3 Tests added for all three HTTP versions covering both the override case and the default behavior case.