X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ffoundation%2Fapi%2Frevel%2Fpanic.go;fp=src%2Ffoundation%2Fapi%2Frevel%2Fpanic.go;h=464f2defecb7fc9e5601494fee4df6eed4c43a63;hb=1d1ee6961c93781e1187d8c7faa868da6b2f01f4;hp=0000000000000000000000000000000000000000;hpb=56dd5e0f2164b37b40ac1daa188ccc618b4cbd19;p=iec.git diff --git a/src/foundation/api/revel/panic.go b/src/foundation/api/revel/panic.go new file mode 100644 index 0000000..464f2de --- /dev/null +++ b/src/foundation/api/revel/panic.go @@ -0,0 +1,50 @@ +// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved. +// Revel Framework source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package revel + +import ( + "fmt" + "net/http" + "runtime/debug" +) + +// PanicFilter wraps the action invocation in a protective defer blanket that +// converts panics into 500 error pages. +func PanicFilter(c *Controller, fc []Filter) { + defer func() { + if err := recover(); err != nil { + handleInvocationPanic(c, err) + } + }() + fc[0](c, fc[1:]) +} + +// This function handles a panic in an action invocation. +// It cleans up the stack trace, logs it, and displays an error page. +func handleInvocationPanic(c *Controller, err interface{}) { + error := NewErrorFromPanic(err) + if error != nil { + utilLog.Error("PanicFilter: Caught panic", "error", err, "stack", error.Stack) + if DevMode { + fmt.Println(err) + fmt.Println(error.Stack) + } + } else { + utilLog.Error("PanicFilter: Caught panic, unable to determine stack location", "error", err, "stack", string(debug.Stack())) + if DevMode { + fmt.Println(err) + fmt.Println("stack", string(debug.Stack())) + } + } + + if error == nil && DevMode { + // Only show the sensitive information in the debug stack trace in development mode, not production + c.Response.SetStatus(http.StatusInternalServerError) + _, _ = c.Response.GetWriter().Write(debug.Stack()) + return + } + + c.Result = c.RenderError(error) +}