Skip to content

Commit

Permalink
fix: return better error codes from proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Sep 27, 2024
1 parent b4941ae commit 8f0110c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f0110c

Please sign in to comment.