What version of Go are you using (go version
)?
$ go version go version go1.11.1 darwin/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GOARCH="amd64" GOBIN="" GOCACHE="/Users/harald/Library/Caches/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/harald/go" GOPROXY="" GORACE="" GOROOT="/usr/local/Cellar/go/1.11.1/libexec" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.11.1/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/w3/11rv94110qnc2l400nlz7gmc0000gn/T/go-build136867624=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you expect to see?
It seems to me that when using ReverseProxy.ErrorHandler (from package httputil
) a nice pattern would be to introspect the error, do special processing on or ignore certain types of errors, but then fall back to the default error handling.
Therefore, I would like to export this function so I don't have to reimplement it (and reimplement logf
):
func (p *ReverseProxy) defaultErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
p.logf("http: proxy error: %v", err)
rw.WriteHeader(http.StatusBadGateway)
}
An added bonus is also that you then receive updates to this logic as Go versions progress, whereas the copied-over code would get stale.
Comment From: HaraldNordgren
My current workaround is to turn the method into a function with the ReverseProxy
as first argument to be able to define it locally:
// Adapted from https://github.com/golang/go/blob/go1.11.4/src/net/http/httputil/reverseproxy.go
func logf(p *httputil.ReverseProxy, format string, args ...interface{}) {
if p.ErrorLog != nil {
p.ErrorLog.Printf(format, args...)
} else {
log.Printf(format, args...)
}
}
// Adapted from https://github.com/golang/go/blob/go1.11.4/src/net/http/httputil/reverseproxy.go
func defaultErrorHandler(p *httputil.ReverseProxy) func(rw http.ResponseWriter, req *http.Request, err error) {
return func(rw http.ResponseWriter, req *http.Request, err error) {
logf(p, "http: proxy error: %v", err)
rw.WriteHeader(http.StatusBadGateway)
}
}