-
Notifications
You must be signed in to change notification settings - Fork 405
/
error.go
295 lines (263 loc) · 8.01 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
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
290
291
292
293
294
295
package models
import (
"errors"
"fmt"
"net/http"
)
// TODO we can put constants all in this file too
const (
maxAppName = 30
maxFnName = 30
MaxTriggerName = 30
)
var (
ErrMethodNotAllowed = err{
code: http.StatusMethodNotAllowed,
error: errors.New("Method not allowed"),
}
ErrInvalidJSON = err{
code: http.StatusBadRequest,
error: errors.New("Invalid JSON"),
}
ErrClientCancel = err{
// The special custom error code to close connection without any response
code: 444,
error: errors.New("Client cancelled context"),
}
ErrCallTimeoutServerBusy = err{
code: http.StatusServiceUnavailable,
error: errors.New("Timed out - server too busy"),
}
ErrUnsupportedMediaType = err{
code: http.StatusUnsupportedMediaType,
error: errors.New("Content Type not supported")}
ErrMissingID = err{
code: http.StatusBadRequest,
error: errors.New("Missing ID")}
ErrMissingAppID = err{
code: http.StatusBadRequest,
error: errors.New("Missing App ID")}
ErrMissingFnID = err{
code: http.StatusBadRequest,
error: errors.New("Missing Fn ID")}
ErrMissingName = err{
code: http.StatusBadRequest,
error: errors.New("Missing Name")}
ErrCreatedAtProvided = err{
code: http.StatusBadRequest,
error: errors.New("Trigger Created At Provided for Create")}
ErrUpdatedAtProvided = err{
code: http.StatusBadRequest,
error: errors.New("Trigger ID Provided for Create")}
ErrDatastoreEmptyApp = err{
code: http.StatusBadRequest,
error: errors.New("Missing app"),
}
ErrDatastoreEmptyCallID = err{
code: http.StatusBadRequest,
error: errors.New("Missing call ID"),
}
ErrDatastoreEmptyFn = err{
code: http.StatusBadRequest,
error: errors.New("Missing Fn"),
}
ErrDatastoreEmptyFnID = err{
code: http.StatusBadRequest,
error: errors.New("Missing Fn ID"),
}
ErrInvalidPayload = err{
code: http.StatusBadRequest,
error: errors.New("Invalid payload"),
}
ErrFoundDynamicURL = err{
code: http.StatusBadRequest,
error: errors.New("Dynamic URL is not allowed"),
}
ErrPathMalformed = err{
code: http.StatusBadRequest,
error: errors.New("Path malformed"),
}
ErrInvalidToTime = err{
code: http.StatusBadRequest,
error: errors.New("to_time is not an epoch time"),
}
ErrInvalidFromTime = err{
code: http.StatusBadRequest,
error: errors.New("from_time is not an epoch time"),
}
ErrInvalidMemory = err{
code: http.StatusBadRequest,
error: fmt.Errorf("memory value is out of range. It should be between 0 and %d", MaxMemory),
}
ErrCallResourceTooBig = err{
code: http.StatusBadRequest,
error: fmt.Errorf("Requested CPU/Memory cannot be allocated"),
}
ErrCallNotFound = err{
code: http.StatusNotFound,
error: errors.New("Call not found"),
}
ErrInvalidCPUs = err{
code: http.StatusBadRequest,
error: fmt.Errorf("Cpus is invalid. Value should be either between [%.3f and %.3f] or [%dm and %dm] milliCPU units",
float64(MinMilliCPUs)/1000.0, float64(MaxMilliCPUs)/1000.0, MinMilliCPUs, MaxMilliCPUs),
}
ErrCallLogNotFound = err{
code: http.StatusNotFound,
error: errors.New("Call log not found"),
}
ErrPathNotFound = err{
code: http.StatusNotFound,
error: errors.New("Path not found"),
}
ErrInvalidAnnotationKey = err{
code: http.StatusBadRequest,
error: errors.New("Invalid annotation key, annotation keys must be non-empty ascii strings excluding whitespace"),
}
ErrInvalidAnnotationKeyLength = err{
code: http.StatusBadRequest,
error: fmt.Errorf("Invalid annotation key length, annotation keys may not be larger than %d bytes", maxAnnotationKeyBytes),
}
ErrInvalidAnnotationValue = err{
code: http.StatusBadRequest,
error: errors.New("Invalid annotation value, annotation values may only be non-empty strings, numbers, objects, or arrays"),
}
ErrInvalidAnnotationValueLength = err{
code: http.StatusBadRequest,
error: fmt.Errorf("Invalid annotation value length, annotation values may not be larger than %d bytes when serialized as JSON", maxAnnotationValueBytes),
}
ErrTooManyAnnotationKeys = err{
code: http.StatusBadRequest,
error: fmt.Errorf("Invalid annotation change, new key(s) exceed maximum permitted number of annotations keys (%d)", maxAnnotationsKeys),
}
ErrTooManyRequests = err{
code: http.StatusTooManyRequests,
error: errors.New("Too many requests submitted"),
}
ErrAsyncUnsupported = err{
code: http.StatusBadRequest,
error: errors.New("Async functions are not supported on this server"),
}
ErrDetachUnsupported = err{
code: http.StatusNotImplemented,
error: errors.New("Detach call functions are not supported on this server"),
}
ErrCallHandlerNotFound = err{
code: http.StatusInternalServerError,
error: errors.New("Unable to find the call handle"),
}
ErrServiceReservationFailure = err{
code: http.StatusInternalServerError,
error: errors.New("Unable to service the request for the reservation period"),
}
// func errors
ErrDockerPullTimeout = ferr{
code: http.StatusGatewayTimeout,
error: errors.New("Docker pull timed out"),
}
ErrFunctionResponseTooBig = ferr{
code: http.StatusBadGateway,
error: fmt.Errorf("function response too large"),
}
ErrFunctionResponse = ferr{
code: http.StatusBadGateway,
error: fmt.Errorf("error receiving function response"),
}
ErrFunctionFailed = ferr{
code: http.StatusBadGateway,
error: fmt.Errorf("function failed"),
}
ErrFunctionInvalidResponse = ferr{
code: http.StatusBadGateway,
error: fmt.Errorf("invalid function response"),
}
ErrRequestContentTooBig = ferr{
code: http.StatusRequestEntityTooLarge,
error: fmt.Errorf("Request content too large"),
}
ErrCallTimeout = ferr{
code: http.StatusGatewayTimeout,
error: errors.New("Timed out"),
}
ErrContainerInitFail = ferr{
code: http.StatusBadGateway,
error: errors.New("container failed to initialize, please ensure you are using the latest fdk / format and check the logs"),
}
ErrContainerInitTimeout = ferr{
code: http.StatusGatewayTimeout,
error: errors.New("Container initialization timed out, please ensure you are using the latest fdk / format and check the logs"),
}
)
// APIError any error that implements this interface will return an API response
// with the provided status code and error message body
type APIError interface {
Code() int
error
}
type err struct {
code int
error
}
var _ APIError = err{}
func (e err) Code() int { return e.code }
// NewAPIError returns an APIError given a code and error
func NewAPIError(code int, e error) APIError { return err{code, e} }
// IsAPIError returns whether err implements APIError
func IsAPIError(e error) bool {
_, ok := e.(APIError)
return ok
}
// GetAPIErrorCode returns 0 if an error is not an APIError, or the result
// of the Code() method from an APIError
func GetAPIErrorCode(e error) int {
err, ok := e.(APIError)
if ok {
return err.Code()
}
return 0
}
// FuncError is an error that is the function's fault, that uses the
// APIError but distinguishes fault to function specific errors
type FuncError interface {
APIError
// hidden method needed for duck typing
userError()
}
type ferr struct {
code int
error
}
var _ FuncError = ferr{}
var _ APIError = ferr{}
func (e ferr) userError() {}
func (e ferr) Code() int { return e.code }
// NewFuncError returns a FuncError
func NewFuncError(err APIError) error { return ferr{code: err.Code(), error: err} }
// IsFuncError checks if err is of type FuncError
func IsFuncError(err error) bool { _, ok := err.(FuncError); return ok }
// ErrorWrapper uniform error output (v1) only
type ErrorWrapper struct {
Error *Error `json:"error,omitempty"`
}
func (m *ErrorWrapper) Validate() error {
return nil
}
// APIErrorWrapper wraps an error with an APIError such that the APIError
// governs the HTTP response but the root error remains accessible.
type APIErrorWrapper interface {
APIError
RootError() error
}
type apiErrorWrapper struct {
APIError
root error
}
func (w apiErrorWrapper) RootError() error {
return w.root
}
func NewAPIErrorWrapper(apiErr APIError, rootErr error) APIErrorWrapper {
return &apiErrorWrapper{
APIError: apiErr,
root: rootErr,
}
}