forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
162 lines (152 loc) · 3.32 KB
/
errors.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
package buffalo
import (
"encoding/json"
"fmt"
"strings"
"github.com/gobuffalo/velvet"
"github.com/pkg/errors"
)
// HTTPError a typed error returned by http Handlers and used for choosing error handlers
type HTTPError struct {
Status int `json:"status"`
Cause error `json:"error"`
}
func (h HTTPError) Error() string {
return h.Cause.Error()
}
// ErrorHandler interface for handling an error for a
// specific status code.
type ErrorHandler func(int, error, Context) error
// ErrorHandlers is used to hold a list of ErrorHandler
// types that can be used to handle specific status codes.
/*
a.ErrorHandlers[500] = func(status int, err error, c buffalo.Context) error {
res := c.Response()
res.WriteHeader(status)
res.Write([]byte(err.Error()))
return nil
}
*/
type ErrorHandlers map[int]ErrorHandler
// Get a registered ErrorHandler for this status code. If
// no ErrorHandler has been registered, a default one will
// be returned.
func (e ErrorHandlers) Get(status int) ErrorHandler {
if eh, ok := e[status]; ok {
return eh
}
return defaultErrorHandler
}
func defaultErrorHandler(status int, err error, c Context) error {
env := c.Value("env")
if env != nil && env.(string) == "production" {
c.Response().WriteHeader(status)
c.Response().Write([]byte(prodErrorTmpl))
return nil
}
c.Logger().Error(err)
c.Response().WriteHeader(status)
msg := fmt.Sprintf("%+v", err)
ct := c.Request().Header.Get("Content-Type")
switch strings.ToLower(ct) {
case "application/json", "text/json", "json":
err = json.NewEncoder(c.Response()).Encode(map[string]interface{}{
"error": msg,
"code": status,
})
case "application/xml", "text/xml", "xml":
default:
data := map[string]interface{}{
"routes": c.Value("routes"),
"error": msg,
"status": status,
"data": c.Data(),
}
ctx := velvet.NewContextWith(data)
t, err := velvet.Render(devErrorTmpl, ctx)
if err != nil {
return errors.WithStack(err)
}
res := c.Response()
res.WriteHeader(404)
_, err = res.Write([]byte(t))
return err
}
return err
}
var devErrorTmpl = `
<html>
<head>
<title>{{status}} - ERROR!</title>
<style>
body {
font-family: helvetica;
}
table {
width: 100%;
}
th {
text-align: left;
}
tr:nth-child(even) {
background-color: #dddddd;
}
td {
margin: 0px;
padding: 10px;
}
pre {
display: block;
padding: 9.5px;
margin: 0 0 10px;
font-size: 13px;
line-height: 1.42857143;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>{{status}} - ERROR!</h1>
<pre>{{error}}</pre>
<hr>
<h3>Context</h3>
<pre>{{#each data as |k v|}}
{{inspect k}}: {{inspect v}}
{{/each}}</pre>
<hr>
<h3>Routes</h3>
<table id="buffalo-routes-table">
<thead>
<tr>
<th>METHOD</th>
<th>PATH</th>
<th>HANDLER</th>
</tr>
</thead>
<tbody>
{{#each routes as |route|}}
<tr>
<td>{{route.Method}}</td>
<td>{{route.Path}}</td>
<td><code>{{route.HandlerName}}</code></td>
</tr>
{{/each}}
</tbody>
</table>
</body>
</html>
`
var prodErrorTmpl = `
<h1>We're Sorry!</h1>
<p>
It looks like something went wrong! Don't worry, we are aware of the problem and are looking into it.
</p>
<p>
Sorry if this has caused you any problems. Please check back again later.
</p>
`