Skip to content
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

net/http/httputil: export ReverseProxy.defaultErrorHandler #29403

Open
HaraldNordgren opened this issue Dec 23, 2018 · 1 comment
Open

net/http/httputil: export ReverseProxy.defaultErrorHandler #29403

HaraldNordgren opened this issue Dec 23, 2018 · 1 comment
Labels
FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@HaraldNordgren
Copy link
Member

HaraldNordgren commented Dec 23, 2018

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.

@mvdan mvdan added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 24, 2018
@mvdan mvdan changed the title Export 'func (p *ReverseProxy) defaultErrorHandler' net/http/httputil: export ReverseProxy.defaultErrorHandler Dec 24, 2018
@HaraldNordgren
Copy link
Member Author

HaraldNordgren commented Dec 28, 2018

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)
	}
}

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants