-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
289 lines (245 loc) · 6.79 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package rmsgo
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
type (
// @todo: to be extended as a RFC 9457 compliant error
// (?maybe as its own library?)
HttpError struct {
Type string `json:"type"` // identifies this problem type, non-resolvable, eg. TAG, OR dereferencable uri to HTML page explaining the error type
Status int `json:"status"` // must be the same as the HTTP response status
Title string `json:"title"` // always the same for this error type (@todo: how to enforce this? store out of band? (map[HttpError]Title))
Detail string `json:"detail"` // contains info specific to this instance of the error
Instance string `json:"instance"` // opaque identifier, eg. to correlate with log statements on the server OR dereferencable uri to fetch problem details object
}
ErrBadRequest struct {
HttpError
}
ErrMethodNotAllowed struct {
HttpError
}
ErrForbidden struct {
HttpError
}
ErrNotFound struct {
HttpError
}
ErrMaybeNotFound struct {
HttpError
Cause error
}
ErrNotAFolder struct {
HttpError
}
ErrNotADocument struct {
HttpError
}
ErrInvalidIfNonMatch struct {
HttpError
}
ErrInvalidIfMatch struct {
HttpError
}
ErrNotModified struct{} // not an RFC9457 error: (1) 304 does not allow a body, (2) also not really an error, just an (expected) condition
ErrConflict struct {
HttpError
}
ErrMaybeAncestorConflict struct {
HttpError
Cause error
}
ErrDocExists struct {
HttpError
}
ErrVersionMismatch struct {
HttpError
}
)
func (e HttpError) Error() string {
return fmt.Sprintf("%#v", e)
}
func (e HttpError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("Content-Type", "application/problem+json") // respond with problem+json even if the client didn't request it (rfc9457#section-3-11)
//text := http.StatusText(e.Status) // @todo: replace with RFC 9457 formatted error response
//http.Error(w, text, e.Status)
// @todo: do we want any validation that the HttpError is valid? (eg., valid Status value, type, ...?)
w.WriteHeader(e.Status)
encErr := json.NewEncoder(w).Encode(e)
if encErr != nil {
// at this point we probably can't respond (eg., with internal server error) anymore
g.unhandled(errors.Join(encErr, e))
}
}
func (e HttpError) RespondError(w http.ResponseWriter, r *http.Request) bool {
e.ServeHTTP(w, r)
return true
}
func BadRequest(msg string) error {
s := http.StatusBadRequest
return ErrBadRequest{
HttpError: HttpError{
Status: s,
Title: http.StatusText(s),
Detail: msg,
},
}
}
func MethodNotAllowed(msg string) error {
s := http.StatusMethodNotAllowed
return ErrMethodNotAllowed{
HttpError: HttpError{
Status: s,
Title: http.StatusText(s),
},
}
}
func Forbidden(msg string) error {
s := http.StatusForbidden
return ErrForbidden{
HttpError: HttpError{
Status: s,
Title: http.StatusText(s),
},
}
}
func NotFound(msg string) error {
s := http.StatusNotFound
return ErrNotFound{
HttpError: HttpError{
Status: s,
Title: http.StatusText(s),
},
}
}
func MaybeNotFound(err error) error {
s := http.StatusNotFound
return ErrMaybeNotFound{
HttpError: HttpError{
Status: s,
Title: http.StatusText(s),
},
Cause: err,
}
}
func (e ErrMaybeNotFound) Unwrap() error {
return e.Cause
}
func (e ErrMaybeNotFound) IsNotFound() bool {
return errors.Is(e.Cause, ErrNotExist) // @todo: other possible not founds?
}
func (e ErrMaybeNotFound) RespondError(w http.ResponseWriter, r *http.Request) bool {
if e.IsNotFound() {
return e.HttpError.RespondError(w, r)
}
return false
}
func NotAFolder(path string) error {
return ErrNotAFolder{
HttpError: HttpError{
Status: http.StatusBadRequest,
Title: "requested resource is not a folder",
Detail: "a request was made to retrieve a folder, but a document with the same path was found",
// @todo: fmt.Sprintf("%s", path),
},
}
}
func NotADocument(path string) error {
return ErrNotADocument{
HttpError: HttpError{
Status: http.StatusBadRequest,
Title: "requested resource is not a document",
Detail: "a request was made to retrieve a document, but a folder with the same path was found",
// @todo: fmt.Sprintf("%s", path),
},
}
}
func InvalidIfNonMatch(cond string) error {
return ErrInvalidIfNonMatch{
HttpError: HttpError{
Status: http.StatusBadRequest,
Title: "invalid etag",
Detail: "the etag contained in the If-None-Match header could not be parsed",
// @todo: fmt.Sprintf("%s", cond),
},
}
}
func InvalidIfMatch(cond string) error {
return ErrInvalidIfMatch{
HttpError: HttpError{
Status: http.StatusBadRequest,
Title: "invalid etag",
Detail: "the etag contained in the If-Match header could not be parsed",
// @todo: fmt.Sprintf("%s", cond),
},
}
}
func NotModified() error {
return ErrNotModified{}
}
func (e ErrNotModified) Error() string {
return http.StatusText(http.StatusNotModified)
}
func (e ErrNotModified) RespondError(w http.ResponseWriter, r *http.Request) bool {
s := http.StatusNotModified
http.Error(w, http.StatusText(s), s)
return true
}
func Conflict(path string) error {
return ErrConflict{
HttpError: HttpError{
Status: http.StatusConflict,
Title: "conflicting path names",
Detail: "the document conflicts with an already existing folder of the same name",
// @todo: fmt.Sprintf("%s", cond),
},
}
}
func MaybeAncestorConflict(err error, path string) error {
return ErrMaybeAncestorConflict{
HttpError: HttpError{
Status: http.StatusConflict,
Title: "conflicting path names while creating ancestors",
Detail: "the name of an ancestor collides with the name of an existing document",
// @todo: fmt.Sprintf("%s", cond),
// err.(ConflictError).ConflictPath
},
Cause: err,
}
}
func (e ErrMaybeAncestorConflict) Unwrap() error {
return e.Cause
}
func (e ErrMaybeAncestorConflict) AsConflict(conflict *ConflictError) bool {
return errors.As(e.Cause, conflict)
}
func (e ErrMaybeAncestorConflict) RespondError(w http.ResponseWriter, r *http.Request) bool {
var errConflict ConflictError
if isConflict := e.AsConflict(&errConflict); isConflict {
return e.HttpError.RespondError(w, r)
}
return false
}
func DocExists(path string) error {
return ErrDocExists{
HttpError: HttpError{
Status: http.StatusPreconditionFailed,
Title: "document already exists",
Detail: "the request was rejected because the requested document already exists, but If-None-Match with a value of * was specified",
// @todo: fmt.Sprintf("%s", cond),
},
}
}
func VersionMismatch(expected, actual ETag) error {
return ErrVersionMismatch{
HttpError: HttpError{
Status: http.StatusPreconditionFailed,
Title: "version mismatch",
Detail: "the version provided in the If-Match header does not match the document's current version",
// @todo: expected, actual
},
}
}