raven-go
raven-go copied to clipboard
net/http: raven.RecoveryHandler cannot be passed into mux.Handle
If I create a new HTTP handler like so:
fooHandler, err := handlers.NewFooHandler()
if err != nil {
log.Fatalf("Error: %s", err)
}
mux := http.NewServeMux()
mux.Handle("/foo/", raven.RecoveryHandler(fooHandler))
I get the following two exceptions:
cannot use fooHandler (type *handlers.SnsBounceHandler) as type func(http.ResponseWriter, *http.Request) in argument to raven.RecoveryHandler
cannot use raven.RecoveryHandler(snsBh) (type func(http.ResponseWriter, *http.Request)) as type http.Handler in argument to mux.Handle:
func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)
It looks like two things are occurring here:
- Custom objects that implement the
http.Handlerinterface are not accepted in theraven.RecoveryHandlerfunction. - One cannot use the
raven.RecoverHandlerfunction withmux.HandlebecauseRecoveryHandlerreturns a function handler instead of ahttp.Handleobject.
Yeah, so RecoveryHandler expects a HandlerFunc not http.Handle. We can probably just add another function that works with http.Handle.
Ideally, we'd swap the API to be: raven.RecoverHandlerFunc and raven.RecoverHandler.
Would you like to submit a PR for this?
I'm also not sure the best way to go about actually making this API change since versions don't really exist.
This was the first thing I ran into when integrating raven-go. The library works great, but that usage definitely doesn't match idiomatic go handlers. I've submitted a PR. If you are worried about breaking changes, you could use something like gopkg.in for versioning.
@strife25 it's not the prettiest solution, but you can get pretty close with the existing implementation by using http.HanderFunc:
fooHandler = http.HandlerFunc(raven.RecoveryHandler(fooHandler.ServeHTTP))
mux.Handle("/foo/", fooHandler)
I suggest wrapping your entire mux, so all handlers get the benefit of reporting. I do something like the following: (handlers.LoggingHandler is from http://www.gorillatoolkit.org/pkg/handlers)
mux := http.NewServeMux()
mux.Handle("/foo/", fooHandler)
mux.Handle("/bar/", barHandler)
// ... other handlers ...
handler := http.HandlerFunc(raven.RecoveryHandler(mux.ServeHTTP))
handler = handlers.LoggingHandler(os.Stdout, handler)
http.ListenAndServe(addr, handler)
mux.Use(raven.Recovery) looks to now be available as of https://github.com/getsentry/raven-go/pull/193