Skip to content

Commit 18e965c

Browse files
committed
fix(error handling): fixed server side error handling page
1 parent e3564d1 commit 18e965c

File tree

3 files changed

+41
-62
lines changed

3 files changed

+41
-62
lines changed

pkg/middleware/middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func (ctx *Context) Handle(status int, title string, err error) {
187187
}
188188

189189
ctx.Data["Title"] = title
190+
ctx.Data["AppSubUrl"] = setting.AppSubUrl
190191
ctx.HTML(status, strconv.Itoa(status))
191192
}
192193

pkg/middleware/recovery.go

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,14 @@ import (
1919
"bytes"
2020
"fmt"
2121
"io/ioutil"
22-
"net/http"
2322
"runtime"
2423

2524
"gopkg.in/macaron.v1"
2625

27-
"github.com/go-macaron/inject"
2826
"github.com/grafana/grafana/pkg/log"
2927
"github.com/grafana/grafana/pkg/setting"
3028
)
3129

32-
const (
33-
panicHtml = `<html>
34-
<head><title>PANIC: %s</title>
35-
<meta charset="utf-8" />
36-
<style type="text/css">
37-
html, body {
38-
font-family: "Roboto", sans-serif;
39-
color: #333333;
40-
background-color: #ea5343;
41-
margin: 0px;
42-
}
43-
h1 {
44-
color: #d04526;
45-
background-color: #ffffff;
46-
padding: 20px;
47-
border-bottom: 1px dashed #2b3848;
48-
}
49-
pre {
50-
margin: 20px;
51-
padding: 20px;
52-
border: 2px solid #2b3848;
53-
background-color: #ffffff;
54-
white-space: pre-wrap; /* css-3 */
55-
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
56-
white-space: -pre-wrap; /* Opera 4-6 */
57-
white-space: -o-pre-wrap; /* Opera 7 */
58-
word-wrap: break-word; /* Internet Explorer 5.5+ */
59-
}
60-
</style>
61-
</head><body>
62-
<h1>PANIC</h1>
63-
<pre style="font-weight: bold;">%s</pre>
64-
<pre>%s</pre>
65-
</body>
66-
</html>`
67-
)
68-
6930
var (
7031
dunno = []byte("???")
7132
centerDot = []byte("·")
@@ -151,21 +112,34 @@ func Recovery() macaron.Handler {
151112

152113
panicLogger.Error("Request error", "error", err, "stack", string(stack))
153114

154-
// Lookup the current responsewriter
155-
val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
156-
res := val.Interface().(http.ResponseWriter)
115+
c.Data["Title"] = "Server Error"
116+
c.Data["AppSubUrl"] = setting.AppSubUrl
157117

158-
// respond with panic message while in development mode
159-
var body []byte
160-
if setting.Env == setting.DEV {
161-
res.Header().Set("Content-Type", "text/html")
162-
body = []byte(fmt.Sprintf(panicHtml, err, err, stack))
118+
if theErr, ok := err.(error); ok {
119+
c.Data["Title"] = theErr.Error()
163120
}
164121

165-
res.WriteHeader(http.StatusInternalServerError)
166-
if nil != body {
167-
res.Write(body)
122+
if setting.Env == setting.DEV {
123+
c.Data["ErrorMsg"] = string(stack)
168124
}
125+
126+
c.HTML(500, "500")
127+
128+
// // Lookup the current responsewriter
129+
// val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
130+
// res := val.Interface().(http.ResponseWriter)
131+
//
132+
// // respond with panic message while in development mode
133+
// var body []byte
134+
// if setting.Env == setting.DEV {
135+
// res.Header().Set("Content-Type", "text/html")
136+
// body = []byte(fmt.Sprintf(panicHtml, err, err, stack))
137+
// }
138+
//
139+
// res.WriteHeader(http.StatusInternalServerError)
140+
// if nil != body {
141+
// res.Write(body)
142+
// }
169143
}
170144
}()
171145

public/views/500.html

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,32 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
66
<meta name="viewport" content="width=device-width">
77

8-
<title>Grafana</title>
8+
<title>Grafana - Error</title>
9+
10+
<link href='[[.AppSubUrl]]/public/css/fonts.min.css' rel='stylesheet' type='text/css'>
11+
12+
<link rel="stylesheet" href="[[.AppSubUrl]]/public/css/grafana.dark.min.css">
913

10-
<link rel="stylesheet" href="[[.AppSubUrl]]/public/css/grafana.dark.min.css" title="Dark">
1114
<link rel="icon" type="image/png" href="[[.AppSubUrl]]/public/img/fav32.png">
1215

16+
<base href="[[.AppSubUrl]]/" />
17+
1318
</head>
1419

1520
<body>
1621

17-
<div class="gf-box" style="margin: 200px auto 0 auto; width: 500px;">
18-
<div class="gf-box-header">
19-
<span class="gf-box-title">
22+
<div class="page-container">
23+
<div class="page-header">
24+
<h1>
2025
Server side error :(
21-
</span>
26+
</h1>
2227
</div>
2328

24-
<div class="gf-box-body">
25-
<h4>[[.Title]]</h4>
26-
[[.ErrorMsg]]
27-
</div>
28-
</div>
29+
<h4>[[.Title]]</h4>
30+
31+
<pre>[[.ErrorMsg]]</pre>
2932

30-
</body>
33+
</div>
34+
</body>
3135

3236
</html>

0 commit comments

Comments
 (0)