From ff660364c63dfe2904450dc16c8dc5ac45c164c9 Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Fri, 27 Sep 2024 10:46:50 +0200 Subject: [PATCH] fix: return better error codes from proxy (#814) --- proxy/proxy.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 42c25735..3b2697a6 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -258,10 +258,20 @@ func (o *options) beforeProxyMiddleware(h http.Handler) http.Handler { } func defaultErrorHandler(w http.ResponseWriter, r *http.Request, err error) { - if !errors.Is(err, context.Canceled) { + switch { + case errors.Is(err, context.Canceled): + w.WriteHeader(499) // http://nginx.org/en/docs/dev/development_guide.html + case isTimeoutError(err): + w.WriteHeader(http.StatusGatewayTimeout) + default: log.Printf("http: proxy error: %v", err) + w.WriteHeader(http.StatusBadGateway) } - w.WriteHeader(http.StatusBadGateway) +} + +func isTimeoutError(err error) bool { + var te interface{ Timeout() bool } = nil + return errors.As(err, &te) && te.Timeout() || errors.Is(err, context.DeadlineExceeded) } // New creates a new Proxy