Export SEBAVALUES for known projects
[iec.git] / src / foundation / api / revel / panic.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package revel
6
7 import (
8         "fmt"
9         "net/http"
10         "runtime/debug"
11 )
12
13 // PanicFilter wraps the action invocation in a protective defer blanket that
14 // converts panics into 500 error pages.
15 func PanicFilter(c *Controller, fc []Filter) {
16         defer func() {
17                 if err := recover(); err != nil {
18                         handleInvocationPanic(c, err)
19                 }
20         }()
21         fc[0](c, fc[1:])
22 }
23
24 // This function handles a panic in an action invocation.
25 // It cleans up the stack trace, logs it, and displays an error page.
26 func handleInvocationPanic(c *Controller, err interface{}) {
27         error := NewErrorFromPanic(err)
28         if error != nil {
29                 utilLog.Error("PanicFilter: Caught panic", "error", err, "stack", error.Stack)
30                 if DevMode {
31                         fmt.Println(err)
32                         fmt.Println(error.Stack)
33                 }
34         } else {
35                 utilLog.Error("PanicFilter: Caught panic, unable to determine stack location", "error", err, "stack", string(debug.Stack()))
36                 if DevMode {
37                         fmt.Println(err)
38                         fmt.Println("stack", string(debug.Stack()))
39                 }
40         }
41
42         if error == nil && DevMode {
43                 // Only show the sensitive information in the debug stack trace in development mode, not production
44                 c.Response.SetStatus(http.StatusInternalServerError)
45                 _, _ = c.Response.GetWriter().Write(debug.Stack())
46                 return
47         }
48
49         c.Result = c.RenderError(error)
50 }