This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy patherrors.go
155 lines (137 loc) · 3.62 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
package buffalo
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"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)
eh := a.ErrorHandlers.Get(500)
eh(500, err, c)
}
}()
return next(c)
}
}
func productionErrorResponseFor(status int) []byte {
if status == http.StatusNotFound {
return []byte(prodNotFoundTmpl)
}
return []byte(prodErrorTmpl)
}
func defaultErrorHandler(status int, origErr error, c Context) error {
env := c.Value("env")
ct := defaults.String(httpx.ContentType(c.Request()), "text/html")
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
}
msg := fmt.Sprintf("%+v", origErr)
switch strings.ToLower(ct) {
case "application/json", "text/json", "json":
err := json.NewEncoder(c.Response()).Encode(map[string]interface{}{
"error": msg,
"code": status,
})
if err != nil {
return errors.WithStack(err)
}
case "application/xml", "text/xml", "xml":
default:
if err := c.Request().ParseForm(); err != nil {
msg = fmt.Sprintf("%s\n%s", err.Error(), msg)
}
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": msg,
"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")
}