-
Notifications
You must be signed in to change notification settings - Fork 34
/
show_errors_middleware.go
187 lines (157 loc) · 4.47 KB
/
show_errors_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package traffic
import (
"bufio"
"html/template"
"os"
"runtime"
"strings"
)
type ShowErrorsMiddleware struct{}
func (middleware ShowErrorsMiddleware) readErrorFileLines(filePath string, errorLine int) map[int]string {
lines := make(map[int]string)
file, err := os.Open(filePath)
if err != nil {
return lines
}
defer file.Close()
reader := bufio.NewReader(file)
currentLine := 0
for {
line, err := reader.ReadString('\n')
if err != nil || currentLine > errorLine+5 {
break
}
currentLine++
if currentLine >= errorLine-5 {
lines[currentLine] = strings.Replace(line, "\n", "", -1)
}
}
return lines
}
func (middleware ShowErrorsMiddleware) RenderError(w ResponseWriter, r *Request, err interface{}, stack []byte) {
_, filePath, line, _ := runtime.Caller(5)
data := map[string]interface{}{
"Error": err,
"Stack": string(stack),
"Params": r.URL.Query(),
"Method": r.Method,
"FilePath": filePath,
"Line": line,
"Lines": middleware.readErrorFileLines(filePath, line),
}
w.Header().Set("Content-Type", "text/html")
tpl := template.Must(template.New("ErrorPage").Parse(panicPageTpl))
tpl.Execute(w, data)
}
func (middleware ShowErrorsMiddleware) ServeHTTP(w ResponseWriter, r *Request, next NextMiddlewareFunc) {
defer func() {
if err := recover(); err != nil {
const size = 4096
stack := make([]byte, size)
stack = stack[:runtime.Stack(stack, false)]
middleware.RenderError(w, r, err, stack)
}
}()
if nextMiddleware := next(); nextMiddleware != nil {
nextMiddleware.ServeHTTP(w, r, next)
}
}
const panicPageTpl string = `
<html>
<head>
<title>Traffic Panic</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
html, body{ padding: 0; margin: 0; }
header { background: #C52F24; color: white; border-bottom: 2px solid #9C0606; }
h1 { padding: 10px 0; margin: 0; }
.container { margin: 0 20px; }
.error { font-size: 18px; background: #FFCCCC; color: #9C0606; padding: 10px 0; }
.file-info .file-name { font-weight: bold; }
.stack { height: 300px; overflow-y: scroll; border: 1px solid #e5e5e5; padding: 10px; }
table.source {
width: 100%;
border-collapse: collapse;
border: 1px solid #e5e5e5;
}
table.source td {
padding: 0;
}
table.source .numbers {
font-size: 14px;
vertical-align: top;
width: 1%;
color: rgba(0,0,0,0.3);
text-align: right;
}
table.source .numbers .number {
display: block;
padding: 0 5px;
border-right: 1px solid #e5e5e5;
}
table.source .numbers .number.line-{{ .Line }} {
border-right: 1px solid #ffcccc;
}
table.source .numbers pre {
white-space: pre-wrap;
}
table.source .code {
font-size: 14px;
vertical-align: top;
}
table.source .code .line {
padding-left: 10px;
display: block;
}
table.source .numbers .number,
table.source .code .line {
padding-top: 1px;
padding-bottom: 1px;
}
table.source .code .line:hover {
background-color: #f6f6f6;
}
table.source .line-{{ .Line }},
table.source line-{{ .Line }},
table.source .code .line.line-{{ .Line }}:hover {
background: #ffcccc;
}
</style>
</head>
<body>
<header>
<div class="container">
<h1>Error</h1>
</div>
</header>
<div class="error">
<p class="container">{{ .Error }}</p>
</div>
<div class="container">
<p class="file-info">
In <span class="file-name">{{ .FilePath }}:{{ .Line }}</span></p>
</p>
<table class="source">
<tr>
<td class="numbers">
<pre>{{ range $lineNumber, $line := .Lines }}<span class="number line-{{ $lineNumber }}">{{ $lineNumber }}</span>{{ end }}</pre>
</td>
<td class="code">
<pre>{{ range $lineNumber, $line := .Lines }}<span class="line line-{{ $lineNumber }}">{{ $line }}<br /></span>{{ end }}</pre>
</td>
</tr>
</table>
<h2>Stack</h2>
<pre class="stack">{{ .Stack }}</pre>
<h2>Request</h2>
<p><strong>Method:</strong> {{ .Method }}</p>
<h3>Paramenters:</h3>
<ul>
{{ range $key, $value := .Params }}
<li><strong>{{ $key }}:</strong> {{ $value }}</li>
{{ end }}
</ul>
</div>
</body>
</html>
`