Skip to content

Custom error handler for httputil.ReverseProxy #14329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sosedoff opened this issue Feb 14, 2016 · 1 comment
Closed

Custom error handler for httputil.ReverseProxy #14329

sosedoff opened this issue Feb 14, 2016 · 1 comment

Comments

@sosedoff
Copy link

Current implementation of httputil.ReverseProxy is pretty handy and useful,
however there's one small issue with error handling: there's no easy way to
override or customize error response.

Here's the current error handling code:

res, err := transport.RoundTrip(outreq)
if err != nil {
  p.logf("http: proxy error: %v", err)
  rw.WriteHeader(http.StatusInternalServerError)
  return
}

Perhaps having a error handler func on ReverseProxy struct might be a good idea:

type ReverseProxy struct {
  // ... existing code ...

  // Handler function that will be invoked when proxy request fails due to 
  // various reasons (timeout, connection refused, etc)
  ErrorHandler func(error, http.ResponseWriter,*http.Request)
}

If ErrorHandler is not provided proxy will keep the existing behavior, otherwise
it'll pass error handling to our custom function.

Example:

proxy := httputil.NewSingleHostReverseProxy(target)
proxy.ErrorHandler = func(err error, w http.ResponseWriter, r *http.Request) {
  // check error type and do something
  // ....
  // respond with some other status code
  w.WriteHeader(http.StatusServiceUnavailable)
}

Any thoughts?

@bradfitz
Copy link
Contributor

I'm folding this into #15034

@golang golang locked and limited conversation to collaborators Apr 10, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants