pkg/web: closure-style middlewares (#51238)
* pkg/web: closure-style middlewares Switches the middleware execution model from web.Handlers in a slice to web.Middleware. Middlewares are temporarily kept in a slice to preserve ordering, but prior to execution they are applied, forming a giant call-stack, giving granular control over the execution flow. * pkg/middleware: adapt to web.Middleware * pkg/middleware/recovery: use c.Req over req c.Req gets updated by future handlers, while req stays static. The current recovery implementation needs this newer information * pkg/web: correct middleware ordering * pkg/webtest: adapt middleware * pkg/web/hack: set w and r onto web.Context By adopting std middlewares, it may happen they invoke next(w,r) without putting their modified w,r into the web.Context, leading old-style handlers to operate on outdated fields. pkg/web now takes care of this * pkg/middleware: selectively use future context * pkg/web: accept closure-style on Use() * webtest: Middleware testing adds a utility function to web/webtest to obtain a http.ResponseWriter, http.Request and http.Handler the same as a middleware that runs would receive * *: cleanup * pkg/web: don't wrap Middleware from Router * pkg/web: require chain to write response * *: remove temp files * webtest: don't require chain write * *: cleanup
This commit is contained in:
+54
-50
@@ -102,69 +102,73 @@ func function(pc uintptr) []byte {
|
||||
|
||||
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
||||
// While Martini is in development mode, Recovery will also output the panic as HTML.
|
||||
func Recovery(cfg *setting.Cfg) web.Handler {
|
||||
return func(c *web.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
var panicLogger log.Logger
|
||||
panicLogger = log.New("recovery")
|
||||
// try to get request logger
|
||||
ctx := contexthandler.FromContext(c.Req.Context())
|
||||
if ctx != nil {
|
||||
panicLogger = ctx.Logger
|
||||
}
|
||||
func Recovery(cfg *setting.Cfg) web.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
c := web.FromContext(req.Context())
|
||||
|
||||
if err, ok := r.(error); ok {
|
||||
// http.ErrAbortHandler is suppressed by default in the http package
|
||||
// and used as a signal for aborting requests. Suppresses stacktrace
|
||||
// since it doesn't add any important information.
|
||||
if errors.Is(err, http.ErrAbortHandler) {
|
||||
panicLogger.Error("Request error", "error", err)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
var panicLogger log.Logger
|
||||
panicLogger = log.New("recovery")
|
||||
// try to get request logger
|
||||
ctx := contexthandler.FromContext(c.Req.Context())
|
||||
if ctx != nil {
|
||||
panicLogger = ctx.Logger
|
||||
}
|
||||
|
||||
if err, ok := r.(error); ok {
|
||||
// http.ErrAbortHandler is suppressed by default in the http package
|
||||
// and used as a signal for aborting requests. Suppresses stacktrace
|
||||
// since it doesn't add any important information.
|
||||
if errors.Is(err, http.ErrAbortHandler) {
|
||||
panicLogger.Error("Request error", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
stack := stack(3)
|
||||
panicLogger.Error("Request error", "error", r, "stack", string(stack))
|
||||
|
||||
// if response has already been written, skip.
|
||||
if c.Resp.Written() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
stack := stack(3)
|
||||
panicLogger.Error("Request error", "error", r, "stack", string(stack))
|
||||
data := struct {
|
||||
Title string
|
||||
AppTitle string
|
||||
AppSubUrl string
|
||||
Theme string
|
||||
ErrorMsg string
|
||||
}{"Server Error", "Grafana", cfg.AppSubURL, cfg.DefaultTheme, ""}
|
||||
|
||||
// if response has already been written, skip.
|
||||
if c.Resp.Written() {
|
||||
return
|
||||
}
|
||||
if setting.Env == setting.Dev {
|
||||
if err, ok := r.(error); ok {
|
||||
data.Title = err.Error()
|
||||
}
|
||||
|
||||
data := struct {
|
||||
Title string
|
||||
AppTitle string
|
||||
AppSubUrl string
|
||||
Theme string
|
||||
ErrorMsg string
|
||||
}{"Server Error", "Grafana", cfg.AppSubURL, cfg.DefaultTheme, ""}
|
||||
|
||||
if setting.Env == setting.Dev {
|
||||
if err, ok := r.(error); ok {
|
||||
data.Title = err.Error()
|
||||
data.ErrorMsg = string(stack)
|
||||
}
|
||||
|
||||
data.ErrorMsg = string(stack)
|
||||
}
|
||||
if ctx != nil && ctx.IsApiRequest() {
|
||||
resp := make(map[string]interface{})
|
||||
resp["message"] = "Internal Server Error - Check the Grafana server logs for the detailed error message."
|
||||
|
||||
if ctx != nil && ctx.IsApiRequest() {
|
||||
resp := make(map[string]interface{})
|
||||
resp["message"] = "Internal Server Error - Check the Grafana server logs for the detailed error message."
|
||||
if data.ErrorMsg != "" {
|
||||
resp["error"] = fmt.Sprintf("%v - %v", data.Title, data.ErrorMsg)
|
||||
} else {
|
||||
resp["error"] = data.Title
|
||||
}
|
||||
|
||||
if data.ErrorMsg != "" {
|
||||
resp["error"] = fmt.Sprintf("%v - %v", data.Title, data.ErrorMsg)
|
||||
ctx.JSON(500, resp)
|
||||
} else {
|
||||
resp["error"] = data.Title
|
||||
ctx.HTML(500, cfg.ErrTemplateName, data)
|
||||
}
|
||||
|
||||
c.JSON(500, resp)
|
||||
} else {
|
||||
c.HTML(500, cfg.ErrTemplateName, data)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
next.ServeHTTP(w, req)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user