Skip to content

Commit

Permalink
fix(http): Deprecate HandleFunc, remove duplication (#260)
Browse files Browse the repository at this point in the history
The Handler.HandleFunc is unnecessary as all uses can be replaced with
Handler.Handle (an http.HandlerFunc implements http.Handler).
  • Loading branch information
rhcarvalho authored Jul 17, 2020
1 parent d099f87 commit 36c7432
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions http/sentryhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,16 @@ func New(options Options) *Handler {

// Handle wraps http.Handler and recovers from caught panics.
func (h *Handler) Handle(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
hub = sentry.CurrentHub().Clone()
}
hub.Scope().SetRequest(r)
ctx = sentry.SetHubOnContext(ctx, hub)
defer h.recoverWithSentry(hub, r)
handler.ServeHTTP(rw, r.WithContext(ctx))
})
return h.handle(handler)
}

// HandleFunc wraps http.HandleFunc and recovers from caught panics.
// Deprecated: Use the Handle method instead.
func (h *Handler) HandleFunc(handler http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
return h.handle(handler)
}

func (h *Handler) handle(handler http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
Expand All @@ -74,8 +68,8 @@ func (h *Handler) HandleFunc(handler http.HandlerFunc) http.HandlerFunc {
hub.Scope().SetRequest(r)
ctx = sentry.SetHubOnContext(ctx, hub)
defer h.recoverWithSentry(hub, r)
handler(rw, r.WithContext(ctx))
}
handler.ServeHTTP(rw, r.WithContext(ctx))
})
}

func (h *Handler) recoverWithSentry(hub *sentry.Hub, r *http.Request) {
Expand Down

0 comments on commit 36c7432

Please sign in to comment.