Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: server/middleware/paniccatcher.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Rebase origin/master Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 » defer paniccatcher.Catch(func(p *paniccatcher.Panic) {
21 » » defer paniccatcher.Catch(func(p *paniccatcher.Panic) { 19 » » log.Fields{
22 » » » log.Fields{ 20 » » » "panic.error": p.Reason,
23 » » » » "panic.error": p.Reason, 21 » » }.Errorf(c.Context, "Caught panic during handling of %q:\n%s", c .Request.RequestURI, p.Stack)
Vadim Sh. 2016/06/18 16:57:20 same as in tsmon middleware: better to preserve th
nishanths 2016/06/19 02:47:02 Done. Now matches behavior at origin/master.
24 » » » }.Errorf(c, "Caught panic during handling of %q:\n%s", r .RequestURI, p.Stack)
25 22
26 » » » // Note: it may be too late to send HTTP 500 if `h` alre ady sent 23 » » // Note: it may be too late to send HTTP 500 if `h` already sent
27 » » » // headers. But there's nothing else we can do at this p oint anyway. 24 » » // 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) 25 » » http.Error(c.Writer, "Internal Server Error. See logs.", http.St atusInternalServerError)
29 » » }) 26 » })
30 » » h(c, w, r, p) 27 » next(c)
31 » }
32 } 28 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698