-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
172 lines (154 loc) · 4.33 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
package mux
import (
"fmt"
"net/http"
)
// Error repesents an error view.
type Error interface {
error
StatusCode() int
}
// Resolver represents the ability to resolve an error to a view.
type Resolver interface {
Resolve(req *http.Request, code int, err error) Error
}
// ResolverFunc is an adapter to allow the use of
// ordinary functions as Resolvers.
type ResolverFunc func(req *http.Request, code int, err error) Error
// Resolve implements the Resolver interface.
func (fn ResolverFunc) Resolve(req *http.Request, code int, err error) Error {
return fn(req, code, err)
}
// ErrorView is the default error view.
type ErrorView struct {
Code int `json:"code"`
Title string `json:"title"`
Message string `json:"message,omitempty"`
RequestID string `json:"request_id"`
}
// Error implements the error interface.
func (v ErrorView) Error() string {
return v.Message
}
// StatusCode implements the mux.Error interface.
func (v ErrorView) StatusCode() int {
return v.Code
}
// NewErrorView returns a new ErrorView.
func NewErrorView(req *http.Request, code int, err error) ErrorView {
return ErrorView{
Code: code,
Title: http.StatusText(code),
Message: ErrorText(code, err),
RequestID: RequestID(req),
}
}
// ErrorText returns supplementary message text for errors.
//
// Explicit descriptions are returned for mux errors. The error text is
// returned for http.StatusUnprocessableEntity status codes and instances
// of mux.ValidationError. The empty string is returned for unknown errors.
func ErrorText(code int, err error) string {
switch code {
case http.StatusUnprocessableEntity:
return err.Error()
case http.StatusInternalServerError:
return "An unexpected error has occurred."
}
switch err {
case ErrDecodeContentType:
return "Invalid content-type header."
case ErrDecodeRequestData:
return "Invalid request data."
}
switch err.(type) {
case ErrMethodNotAllowed:
return "The method is not allowed for the requested URL."
case ValidationError:
return err.Error()
}
return ""
}
// ErrRedirect represents a redirect response.
type ErrRedirect struct {
URL string
Code int
}
// Error implements the error interface.
func (e ErrRedirect) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.URL)
}
// Panic is an error resolved from a panic with a stack trace.
type Panic struct {
err interface{}
stack []byte
}
// Error implements the error interface.
func (e Panic) Error() string {
err, ok := e.err.(error)
if ok {
return err.Error()
}
return e.err.(string)
}
// String implements the fmt.Stringer interface.
func (e Panic) String() string {
return string(e.stack)
}
// Abort resolves an error to a view and encodes the response.
func (h *Handler) Abort(w http.ResponseWriter, req *http.Request, err error) {
switch err {
case nil:
return
case ErrEncodeMatch:
abort(w, http.StatusNotAcceptable)
return
}
defer h.observer.Abort(req)
redirect, ok := err.(ErrRedirect)
if ok {
http.Redirect(w, req, redirect.URL, redirect.Code)
return
}
view := h.resolve(w, req, err)
code := view.StatusCode()
if code == http.StatusInternalServerError {
h.log(req, err)
}
err = h.Encode(w, req, view, code)
if err != nil {
h.log(req, err)
abort(w, http.StatusInternalServerError)
}
}
// resolve resolves errors to an error view.
func (h *Handler) resolve(w http.ResponseWriter, req *http.Request, err error) Error {
switch err {
case ErrNotFound:
return h.resolver.Resolve(req, http.StatusNotFound, err)
case ErrDecodeContentType:
return h.resolver.Resolve(req, http.StatusUnsupportedMediaType, err)
case ErrDecodeRequestData:
return h.resolver.Resolve(req, http.StatusBadRequest, err)
}
switch e := err.(type) {
case Error:
return e
case ErrMethodNotAllowed:
allowed := err.Error()
w.Header().Set("Allow", allowed)
return h.resolver.Resolve(req, http.StatusMethodNotAllowed, err)
case ValidationError:
return h.resolver.Resolve(req, http.StatusUnprocessableEntity, err)
}
return h.resolver.Resolve(req, http.StatusInternalServerError, err)
}
// abort replies to the request with a plain text error message.
func abort(w http.ResponseWriter, code int) error {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
message := http.StatusText(code)
_, err := fmt.Fprintln(w, message)
return err
}