| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package middleware | 5 package middleware |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 | 9 |
| 10 "github.com/julienschmidt/httprouter" | |
| 11 "golang.org/x/net/context" | |
| 12 | |
| 13 log "github.com/luci/luci-go/common/logging" | 10 log "github.com/luci/luci-go/common/logging" |
| 14 "github.com/luci/luci-go/common/paniccatcher" | 11 "github.com/luci/luci-go/common/paniccatcher" |
| 12 "github.com/luci/luci-go/server/router" |
| 15 ) | 13 ) |
| 16 | 14 |
| 17 // WithPanicCatcher is a middleware that catches panics, dumps stack trace to | 15 // WithPanicCatcher is a middleware that catches panics, dumps stack trace to |
| 18 // logging and returns HTTP 500. | 16 // logging and returns HTTP 500. |
| 19 func WithPanicCatcher(h Handler) Handler { | 17 func WithPanicCatcher(c *router.Context, next router.Handler) { |
| 20 » return func(c context.Context, w http.ResponseWriter, r *http.Request, p
httprouter.Params) { | 18 » ctx := c.Context |
| 21 » » defer paniccatcher.Catch(func(p *paniccatcher.Panic) { | 19 » w := c.Writer |
| 22 » » » log.Fields{ | 20 » req := c.Request |
| 23 » » » » "panic.error": p.Reason, | 21 » defer paniccatcher.Catch(func(p *paniccatcher.Panic) { |
| 24 » » » }.Errorf(c, "Caught panic during handling of %q:\n%s", r
.RequestURI, p.Stack) | 22 » » log.Fields{ |
| 23 » » » "panic.error": p.Reason, |
| 24 » » }.Errorf(ctx, "Caught panic during handling of %q:\n%s", req.Req
uestURI, p.Stack) |
| 25 | 25 |
| 26 » » » // Note: it may be too late to send HTTP 500 if `h` alre
ady sent | 26 » » // Note: it may be too late to send HTTP 500 if `next` already s
ent |
| 27 » » » // headers. But there's nothing else we can do at this p
oint anyway. | 27 » » // headers. But there's nothing else we can do at this point any
way. |
| 28 » » » http.Error(w, "Internal Server Error. See logs.", http.S
tatusInternalServerError) | 28 » » http.Error(w, "Internal Server Error. See logs.", http.StatusInt
ernalServerError) |
| 29 » » }) | 29 » }) |
| 30 » » h(c, w, r, p) | 30 » next(c) |
| 31 » } | |
| 32 } | 31 } |
| OLD | NEW |