MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / Recover

Function Recover

pkg/webmiddleware/recover.go:32–52  ·  view source on GitHub ↗

Recover returns middleware that recovers from panics.

()

Source from the content-addressed store, hash-verified

30
31// Recover returns middleware that recovers from panics.
32func Recover() MiddlewareFunc {
33 return func(next http.Handler) http.Handler {
34 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35 defer func() {
36 if p := recover(); p != nil {
37 fmt.Fprintln(os.Stderr, p)
38 os.Stderr.Write(debug.Stack())
39 var err error
40 if pErr, ok := p.(error); ok {
41 err = errHTTPRecovered.WithCause(pErr)
42 } else {
43 err = errHTTPRecovered.WithAttributes("panic", p)
44 }
45 log.FromContext(r.Context()).WithError(err).Error("Handler panicked")
46 webhandlers.Error(w, r, err)
47 }
48 }()
49 next.ServeHTTP(w, r)
50 })
51 }
52}

Callers 4

NewFunction · 0.92
NewServerFunction · 0.92
NewFunction · 0.92
TestRecoverFunction · 0.85

Calls 9

FromContextFunction · 0.92
ErrorFunction · 0.92
ErrorMethod · 0.65
WithErrorMethod · 0.65
ContextMethod · 0.65
WriteMethod · 0.45
WithCauseMethod · 0.45
WithAttributesMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 1

TestRecoverFunction · 0.68