-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.ts
357 lines (307 loc) · 9.28 KB
/
error.ts
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2023-2024 the Sequoia authors. All rights reserved. MIT license.
import { Context } from './context.ts'
import { HTTPStatus } from './status.ts'
import { HTMLErrorTemplate } from './util.ts'
import { HTTPResponse } from './httpresponse.ts'
import { ContentType } from './media.ts'
export class SequoiaError extends Error {
constructor(message: string) {
super(message)
this.name = 'SequoiaError'
}
}
export class HTTPError extends Error {
readonly code: HTTPStatus
constructor(code: HTTPStatus, message: string) {
super(message)
this.name = 'HTTPError'
this.code = code
}
}
export class BadRequestError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.BAD_REQUEST, message)
this.name = 'BadRequestError'
}
}
export class UnauthorizedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.UNAUTHORIZED, message)
this.name = 'UnauthorizedError'
}
}
export class PaymentRequiredError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.PAYMENT_REQUIRED, message)
this.name = 'PaymentRequiredError'
}
}
export class ForbiddenError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.FORBIDDEN, message)
this.name = 'ForbiddenError'
}
}
export class NotFoundError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.NOT_FOUND, message)
this.name = 'NotFoundError'
}
}
export class NotAllowedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.NOT_ALLOWED, message)
this.name = 'NotAllowedError'
}
}
export class NotAcceptableError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.NOT_ACCEPTABLE, message)
this.name = 'NotAcceptableError'
}
}
export class ProxyAuthRequiredError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.PROXY_AUTH_REQUIRED, message)
this.name = 'ProxyAuthRequiredError'
}
}
export class TimeoutError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.TIMEOUT, message)
this.name = 'TimeoutError'
}
}
export class ConflictError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.CONFLICT, message)
this.name = 'ConflictError'
}
}
export class GoneError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.GONE, message)
this.name = 'GoneError'
}
}
export class LengthRequiredError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.LENGTH_REQUIRED, message)
this.name = 'LengthRequiredError'
}
}
export class PreconditionFailedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.PRECONDITION_FAILED, message)
this.name = 'PreconditionFailedError'
}
}
export class PayloadTooLargeError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.PAYLOAD_TOO_LARGE, message)
this.name = 'PayloadTooLargeError'
}
}
export class UriTooLongError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.URI_TOO_LONG, message)
this.name = 'UriTooLongError'
}
}
export class UnsupportedMediaError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.UNSUPPORTED_MEDIA, message)
this.name = 'UnsupportedMediaError'
}
}
export class RangeNotSatisfiedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.RANGE_NOT_SATISFIED, message)
this.name = 'RangeNotSatisfiedError'
}
}
export class ExpectationFailedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.EXPECTATION_FAILED, message)
this.name = 'ExpectationFailedError'
}
}
export class TeapotError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.IM_A_TEAPOT, message)
this.name = 'TeapotError'
}
}
export class MisdirectedRequestError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.MISDIRECTED_REQUEST, message)
this.name = 'MisdirectedRequestError'
}
}
export class UnsupportedEntityError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.UNSUPPORTED_ENTITY, message)
this.name = 'UnsupportedEntityError'
}
}
export class LockedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.LOCKED, message)
this.name = 'LockedError'
}
}
export class FailedDependencyError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.FAILED_DEPENDENCY, message)
this.name = 'FailedDependencyError'
}
}
export class TooEarlyError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.TOO_EARLY, message)
this.name = 'TooEarlyError'
}
}
export class UpgradeRequiredError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.UPGRADE_REQUIRED, message)
this.name = 'UpgradeRequiredError'
}
}
export class PreconditionRequiredError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.PRECONDITION_REQUIRED, message)
this.name = 'PreconditionRequiredError'
}
}
export class TooManyRequestsError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.TOO_MANY_REQUESTS, message)
this.name = 'TooManyRequestsError'
}
}
export class HeadersFieldsTooLargeError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.HEADERS_FIELDS_TOO_LARGE, message)
this.name = 'HeadersFieldsTooLargeError'
}
}
export class UnavailableError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.UNAVAILABLE, message)
this.name = 'UnavailableError'
}
}
export class InternalError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.INTERNAL_ERROR, message)
this.name = 'InternalError'
}
}
export class NotImplementedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.NOT_IMPLEMENTED, message)
this.name = 'NotImplementedError'
}
}
export class BadGatewayError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.BAD_GATEWAY, message)
this.name = 'BadGatewayError'
}
}
export class ServiceUnavailableError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.SERVICE_UNAVAILABLE, message)
this.name = 'ServiceUnavailableError'
}
}
export class GatewayTimeoutError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.GATEWAY_TIMEOUT, message)
this.name = 'GatewayTimeoutError'
}
}
export class HttpVersionNotSupportedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.HTTP_VERSION_NOT_SUPPORTED, message)
this.name = 'HttpVersionNotSupportedError'
}
}
export class VariantNegotiatesError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.VARIANT_NEGOTIATES, message)
this.name = 'VariantNegotiatesError'
}
}
export class InsufficientStorageError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.INSUFFICIENT_STORAGE, message)
this.name = 'InsufficientStorageError'
}
}
export class LoopDetectedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.LOOP_DETECTED, message)
this.name = 'LoopDetectedError'
}
}
export class NotExtendedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.NOT_EXTENDED, message)
this.name = 'NotExtendedError'
}
}
export class NetworkAuthFailedError extends HTTPError {
constructor(message: string) {
super(HTTPStatus.NETWORK_AUTH_FAILED, message)
this.name = 'NetworkAuthFailedError'
}
}
export type ErrorHandler = (
context: Context,
error: HTTPError,
) => Promise<HTTPResponse> | HTTPResponse
export const defaultErrorHandler: ErrorHandler = (_ctx: Context, error: HTTPError) => {
return new HTTPResponse({
status: error.code,
type: ContentType.HTML,
body: HTMLErrorTemplate(error),
})
}
export const error400: HTTPResponse = new HTTPResponse({
status: HTTPStatus.BAD_REQUEST,
type: 'text/html; charset=UTF-8',
body: HTMLErrorTemplate(
new BadRequestError(
'Bad request',
),
),
})
export const error403: HTTPResponse = new HTTPResponse({
status: HTTPStatus.FORBIDDEN,
type: 'text/html; charset=UTF-8',
body: HTMLErrorTemplate(
new ForbiddenError(
'Forbidden',
),
),
})
export const error404: HTTPResponse = new HTTPResponse({
status: HTTPStatus.NOT_FOUND,
type: 'text/html; charset=UTF-8',
body: HTMLErrorTemplate(
new NotFoundError(
'The page was not found',
),
),
})
export const error500: HTTPResponse = new HTTPResponse({
status: HTTPStatus.INTERNAL_ERROR,
type: 'text/html; charset=UTF-8',
body: HTMLErrorTemplate(
new InternalError(
'Internal server error',
),
),
})