f4a64502 by astaxie

add error show page

1 parent ddc3d7d5
1 package beego
2
3 import (
4 "fmt"
5 "html/template"
6 "net/http"
7 "runtime"
8 )
9
10 var tpl = `
11 <!DOCTYPE html>
12 <html>
13 <head>
14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
15 <title>beego application error</title>
16 <style>
17 html, body, body * {padding: 0; margin: 0;}
18 #header {background:#ffd; border-bottom:solid 2px #A31515; padding: 20px 10px;}
19 #header h2{ }
20 #footer {border-top:solid 1px #aaa; padding: 5px 10px; font-size: 12px; color:green;}
21 #content {padding: 5px;}
22 #content .stack b{ font-size: 13px; color: red;}
23 #content .stack pre{padding-left: 10px;}
24 table {}
25 td.t {text-align: right; padding-right: 5px; color: #888;}
26 </style>
27 <script type="text/javascript">
28 </script>
29 </head>
30 <body>
31 <div id="header">
32 <h2>{{.AppError}}</h2>
33 </div>
34 <div id="content">
35 <table>
36 <tr>
37 <td class="t">Request Method: </td><td>{{.RequestMethod}}</td>
38 </tr>
39 <tr>
40 <td class="t">Request URL: </td><td>{{.RequestURL}}</td>
41 </tr>
42 <tr>
43 <td class="t">RemoteAddr: </td><td>{{.RemoteAddr }}</td>
44 </tr>
45 </table>
46 <div class="stack">
47 <b>Stack</b>
48 <pre>{{.Stack}}</pre>
49 </div>
50 </div>
51 <div id="footer">
52 <p>beego {{ .BeegoVersion }} (beego framework)</p>
53 <p>golang version: {{.GoVersion}}</p>
54 </div>
55 </body>
56 </html>
57 `
58
59 func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) {
60 t, err := template.New("beegoerrortemp").Parse(tpl)
61 data := make(map[string]string)
62 data["AppError"] = AppName + ":" + fmt.Sprint(err)
63 data["RequestMethod"] = r.Method
64 data["RequestURL"] = r.RequestURI
65 data["RemoteAddr"] = r.RemoteAddr
66 data["Stack"] = Stack
67 data["BeegoVersion"] = VERSION
68 data["GoVersion"] = runtime.Version()
69 t.Execute(rw, data)
70 }
1 package beego 1 package beego
2 2
3 import ( 3 import (
4 "fmt"
4 "net/http" 5 "net/http"
5 "net/url" 6 "net/url"
6 "reflect" 7 "reflect"
...@@ -190,13 +191,20 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -190,13 +191,20 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
190 // go back to panic 191 // go back to panic
191 panic(err) 192 panic(err)
192 } else { 193 } else {
194 var stack string
193 Critical("Handler crashed with error", err) 195 Critical("Handler crashed with error", err)
194 for i := 1; ; i += 1 { 196 for i := 1; ; i++ {
195 _, file, line, ok := runtime.Caller(i) 197 _, file, line, ok := runtime.Caller(i)
196 if !ok { 198 if !ok {
197 break 199 break
198 } 200 }
199 Critical(file, line) 201 Critical(file, line)
202 if RunMode == "dev" {
203 stack = stack + fmt.Sprintln(file, line)
204 }
205 }
206 if RunMode == "dev" {
207 ShowErr(err, rw, r, stack)
200 } 208 }
201 } 209 }
202 } 210 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!