Skip to content

Commit

Permalink
extract AutoHandleHttpPanics so that other packages can use it too (f…
Browse files Browse the repository at this point in the history
…or building compatibility layers)
  • Loading branch information
ntbosscher committed Dec 18, 2020
1 parent fe95b57 commit adb6dda
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions res/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,23 @@ func (f *funcServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

func WrapHTTPFunc(handler HandlerFunc2) http.HandlerFunc {
return func(wr http.ResponseWriter, req *http.Request) {
defer er.HandleErrors(func(input *er.HandlerInput) {
res := &responder{
status: input.SuggestedHttpCode,
data: errorData(input.Message, input.StackTrace),
}

res.Respond(wr, req)
})

defer AutoHandleHttpPanics(wr, req)
res := handler(NewRequest(wr, req))
res.Respond(wr, req)
}
}

func AutoHandleHttpPanics(wr http.ResponseWriter, req *http.Request) {
er.HandleErrors(func(input *er.HandlerInput) {
res := &responder{
status: input.SuggestedHttpCode,
data: errorData(input.Message, input.StackTrace),
}

res.Respond(wr, req)
})
}

func NewRequest(wr http.ResponseWriter, req *http.Request) *Request {
return &Request{
req: req,
Expand Down

0 comments on commit adb6dda

Please sign in to comment.