-
Notifications
You must be signed in to change notification settings - Fork 30
/
error.go
204 lines (187 loc) · 4.34 KB
/
error.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package golf
import (
"bytes"
"fmt"
"html/template"
"net/http"
"net/http/httputil"
"reflect"
"runtime"
"strconv"
)
const errorTemplate = `<!DOCTYPE HTML><html><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Error: {{ .Code }} {{ .Title }}</title>
<style type="text/css" media="screen">
html,body{
padding:0;
margin:0;
font-family: Tahoma;
color: #34495e;
}
h1 {
color: #fff;
margin: 0;
}
.container {
max-width: 1220px;
margin: 0 auto;
padding: 0 20px;
}
#header {
display: block;
background-color: #3498db;
height: 120px;
width: 100%;
}
#title {
padding: 40px 0;
}
.error {
color: #c0392b;
}
pre code {
font-family: "Lucida Console", Monaco, monospace;
}
pre.request-dump {
background-color: #eeeeee;
padding: 20px 20px 0 20px;
overflow: auto;
}
#backtrace {
list-style: none;
padding-left: 0;
font-family: "Lucida Console", Monaco, monospace;
}
#backtrace li {
border-left: 5px solid #61A8DC;
padding-left: 20px;
}
#backtrace .file {
color: #61A8DC;
}
#backtrace .lineno {
color: #ff8a00;
}
#backtrace .method {
color: #34a853;
}
</style>
<body>
<div id="header">
<div class="container">
<div id="title">
<h1>Error: {{ .Code }} {{ .Title }}</h1>
</div>
</div>
</div>
<div class="container">
<p>Sorry, the requested URL caused an error: </p>
<pre><code>{{ .Message }}</code></pre>
<h2>HTTP Request</h2>
<pre class="request-dump">{{ .HTTPRequest }}</pre>
{{ if .StackTrace }}
<h2>Traceback</h2>
<ul id="backtrace">
{{ range .StackTrace }}
<li><p><span class="file">{{ .File }}:</span><span class="lineno">{{ .Number }}</span> <span class="method">{{ .Method }}</span></p></li>
{{ end }}
</ul>
{{ end }}
</div>
</body>
</html>`
const maxFrames = 20
var tmpl = template.New("error")
// The default error handler
func defaultErrorHandler(ctx *Context, data ...map[string]interface{}) {
var renderData map[string]interface{}
if len(data) == 0 {
renderData = make(map[string]interface{})
renderData["Code"] = ctx.statusCode
renderData["Title"] = http.StatusText(ctx.statusCode)
renderData["Message"] = http.StatusText(ctx.statusCode)
} else {
renderData = data[0]
}
if _, ok := renderData["Code"]; !ok {
renderData["Code"] = ctx.statusCode
}
if _, ok := renderData["Title"]; !ok {
renderData["Title"] = http.StatusText(ctx.statusCode)
}
if _, ok := renderData["Message"]; !ok {
renderData["Message"] = http.StatusText(ctx.statusCode)
}
httpRequest, _ := httputil.DumpRequest(ctx.Request, true)
renderData["HTTPRequest"] = string(httpRequest)
var buf bytes.Buffer
tmpl.Parse(errorTemplate)
tmpl.Execute(&buf, renderData)
ctx.Send(&buf)
}
// Frame represent a stack frame inside of a Honeybadger backtrace.
type Frame struct {
Number string `json:"number"`
File string `json:"file"`
Method string `json:"method"`
}
// Error provides more structured information about a Go error.
type Error struct {
err interface{}
Message string
Class string
Stack []*Frame
}
// Error returns the error message
func (e Error) Error() string {
return e.Message
}
// StackTraceString returns the stack trace in a string format.
func (e Error) StackTraceString() string {
buf := new(bytes.Buffer)
for _, v := range e.Stack {
fmt.Fprintf(buf, "%s: %s\n\t%s\n", v.File, v.Number, v.Method)
}
return string(buf.Bytes())
}
// Errorf returns an templateError.
func Errorf(format string, parameters ...interface{}) error {
return fmt.Errorf(format, parameters...)
}
// NewError creates a new error instance
func NewError(msg interface{}) Error {
var err error
switch t := msg.(type) {
case Error:
return t
case error:
err = t
default:
err = fmt.Errorf("%v", t)
}
return Error{
err: err,
Message: err.Error(),
Class: reflect.TypeOf(err).String(),
Stack: generateStack(3),
}
}
func generateStack(offset int) (frames []*Frame) {
stack := make([]uintptr, maxFrames)
length := runtime.Callers(2+offset, stack[:])
for _, pc := range stack[:length] {
f := runtime.FuncForPC(pc)
if f == nil {
continue
}
file, line := f.FileLine(pc)
frame := &Frame{
File: file,
Number: strconv.Itoa(line),
Method: f.Name(),
}
frames = append(frames, frame)
}
return
}