forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
222 lines (201 loc) · 5.2 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package buffalo
import (
"database/sql"
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"sort"
"strings"
"github.com/gobuffalo/events"
"github.com/gobuffalo/plush"
"github.com/gobuffalo/x/defaults"
"github.com/gobuffalo/x/httpx"
"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
}
// PanicHandler recovers from panics gracefully and calls
// the error handling code for a 500 error.
func (a *App) PanicHandler(next Handler) Handler {
return func(c Context) error {
defer func() { //catch or finally
r := recover()
var err error
if r != nil { //catch
switch t := r.(type) {
case error:
err = t
case string:
err = errors.New(t)
default:
err = errors.New(fmt.Sprint(t))
}
err = errors.WithStack(err)
events.EmitError(events.ErrPanic, err,
map[string]interface{}{
"context": c,
"app": a,
},
)
eh := a.ErrorHandlers.Get(500)
eh(500, err, c)
}
}()
return next(c)
}
}
func (a *App) defaultErrorMiddleware(next Handler) Handler {
return func(c Context) error {
err := next(c)
if err == nil {
return nil
}
status := 500
// unpack root cause and check for HTTPError
cause := errors.Cause(err)
switch cause {
case sql.ErrNoRows:
status = 404
default:
if h, ok := cause.(HTTPError); ok {
status = h.Status
}
}
payload := events.Payload{
"context": c,
"app": a,
}
events.EmitError(events.ErrGeneral, err, payload)
eh := a.ErrorHandlers.Get(status)
err = eh(status, err, c)
if err != nil {
events.Emit(events.Event{
Kind: EvtFailureErr,
Message: "unable to handle error and giving up",
Error: err,
Payload: payload,
})
// things have really hit the fan if we're here!!
a.Logger.Error(err)
c.Response().WriteHeader(500)
c.Response().Write([]byte(err.Error()))
}
return nil
}
}
func productionErrorResponseFor(status int) []byte {
if status == http.StatusNotFound {
return []byte(prodNotFoundTmpl)
}
return []byte(prodErrorTmpl)
}
// ErrorResponse is a used to display errors as JSON or XML
type ErrorResponse struct {
XMLName xml.Name `json:"-" xml:"response"`
Error string `json:"error" xml:"error"`
Trace string `json:"trace" xml:"trace"`
Code int `json:"code" xml:"code,attr"`
}
func defaultErrorHandler(status int, origErr error, c Context) error {
env := c.Value("env")
ct := defaults.String(httpx.ContentType(c.Request()), "text/html; charset=utf-8")
c.Response().Header().Set("content-type", ct)
c.Logger().Error(origErr)
c.Response().WriteHeader(status)
if env != nil && env.(string) == "production" {
responseBody := productionErrorResponseFor(status)
c.Response().Write(responseBody)
return nil
}
trace := fmt.Sprintf("%+v", origErr)
switch strings.ToLower(ct) {
case "application/json", "text/json", "json":
err := json.NewEncoder(c.Response()).Encode(&ErrorResponse{
Error: errors.Cause(origErr).Error(),
Trace: trace,
Code: status,
})
if err != nil {
return errors.WithStack(err)
}
case "application/xml", "text/xml", "xml":
err := xml.NewEncoder(c.Response()).Encode(&ErrorResponse{
Error: errors.Cause(origErr).Error(),
Trace: trace,
Code: status,
})
if err != nil {
return errors.WithStack(err)
}
default:
if err := c.Request().ParseForm(); err != nil {
trace = fmt.Sprintf("%s\n%s", err.Error(), trace)
}
routes := c.Value("routes")
if cd, ok := c.(*DefaultContext); ok {
delete(cd.data, "app")
delete(cd.data, "routes")
}
data := map[string]interface{}{
"routes": routes,
"error": trace,
"status": status,
"data": c.Data(),
"params": c.Params(),
"posted_form": c.Request().Form,
"context": c,
"headers": inspectHeaders(c.Request().Header),
"inspect": func(v interface{}) string {
return fmt.Sprintf("%+v", v)
},
}
ctx := plush.NewContextWith(data)
t, err := plush.Render(devErrorTmpl, ctx)
if err != nil {
return errors.WithStack(err)
}
res := c.Response()
_, err = res.Write([]byte(t))
return err
}
return nil
}
type inspectHeaders http.Header
func (i inspectHeaders) String() string {
bb := make([]string, 0, len(i))
for k, v := range i {
bb = append(bb, fmt.Sprintf("%s: %s", k, v))
}
sort.Strings(bb)
return strings.Join(bb, "\n\n")
}