Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Change RecoveryHandler to accept and return http.Handler #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Change RecoveryHandler to accept and return http.Handler
  • Loading branch information
tdterry committed Sep 13, 2016
commit 68f123b09c6ea84b172e933e8569912ed00a3a23
26 changes: 19 additions & 7 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ func (h *Http) Class() string { return "request" }

// Recovery handler to wrap the stdlib net/http Mux.
// Example:
// http.HandleFunc("/", raven.RecoveryHandler(func(w http.ResponseWriter, r *http.Request) {
// ...
// }))
func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// mux := http.NewServeMux()
// // add routes to the mux
//
// handler := RecoveryHandler(mux)
// http.ListenAndServe(addr, handler)
//
func RecoveryHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
Expand All @@ -79,6 +82,15 @@ func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http
}
}()

handler(w, r)
}
handler.ServeHTTP(w, r)
})
}

// Convenience function to wrap http.HandleFunc-style handlers
// Example:
// http.HandleFunc("/", raven.RecoveryHandler(func(w http.ResponseWriter, r *http.Request) {
// ...
// }))
func RecoveryHandlerFunc(f func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return RecoveryHandler(http.HandlerFunc(f)).ServeHTTP
}