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

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: Update tests 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() router.Handler {
20 » return func(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) { 18 » return func(c *router.Context) {
21 defer paniccatcher.Catch(func(p *paniccatcher.Panic) { 19 defer paniccatcher.Catch(func(p *paniccatcher.Panic) {
22 log.Fields{ 20 log.Fields{
23 "panic.error": p.Reason, 21 "panic.error": p.Reason,
24 » » » }.Errorf(c, "Caught panic during handling of %q:\n%s", r .RequestURI, p.Stack) 22 » » » }.Errorf(c.Context, "Caught panic during handling of %q: \n%s", c.Request.RequestURI, p.Stack)
25 23
26 // Note: it may be too late to send HTTP 500 if `h` alre ady sent 24 // Note: it may be too late to send HTTP 500 if `h` alre ady sent
27 // headers. But there's nothing else we can do at this p oint anyway. 25 // headers. But there's nothing else we can do at this p oint anyway.
28 » » » http.Error(w, "Internal Server Error. See logs.", http.S tatusInternalServerError) 26 » » » http.Error(c.Writer, "Internal Server Error. See logs.", http.StatusInternalServerError)
29 }) 27 })
30 h(c, w, r, p)
31 } 28 }
iannucci 2016/06/13 19:23:29 does this still work? I don't think this works any
32 } 29 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698