@nextrush/errors 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +681 -0
- package/dist/index.d.ts +583 -0
- package/dist/index.js +1021 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
- package/src/__tests__/base.test.ts +180 -0
- package/src/__tests__/factory.test.ts +312 -0
- package/src/__tests__/http-errors.test.ts +458 -0
- package/src/__tests__/middleware.test.ts +385 -0
- package/src/__tests__/validation.test.ts +315 -0
- package/src/base.ts +162 -0
- package/src/factory.ts +205 -0
- package/src/http-errors.ts +409 -0
- package/src/index.ts +101 -0
- package/src/middleware.ts +183 -0
- package/src/validation.ts +210 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1021 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/base.ts
|
|
5
|
+
var V8Error = Error;
|
|
6
|
+
var NextRushError = class extends Error {
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "NextRushError");
|
|
9
|
+
}
|
|
10
|
+
/** HTTP status code */
|
|
11
|
+
status;
|
|
12
|
+
/** Error code for programmatic handling */
|
|
13
|
+
code;
|
|
14
|
+
/** Whether error message is safe to expose to client */
|
|
15
|
+
expose;
|
|
16
|
+
/** Additional error details */
|
|
17
|
+
details;
|
|
18
|
+
/** Original error that caused this error */
|
|
19
|
+
cause;
|
|
20
|
+
constructor(message, options = {}) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = this.constructor.name;
|
|
23
|
+
this.status = options.status ?? 500;
|
|
24
|
+
this.code = options.code ?? "INTERNAL_ERROR";
|
|
25
|
+
this.expose = options.expose ?? this.status < 500;
|
|
26
|
+
this.details = options.details;
|
|
27
|
+
this.cause = options.cause;
|
|
28
|
+
if (V8Error.captureStackTrace && !(this.expose && this.status < 500)) {
|
|
29
|
+
V8Error.captureStackTrace(this, this.constructor);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Convert error to JSON representation
|
|
34
|
+
*/
|
|
35
|
+
toJSON() {
|
|
36
|
+
const json = {
|
|
37
|
+
error: this.name,
|
|
38
|
+
message: this.expose ? this.message : "Internal Server Error",
|
|
39
|
+
code: this.code,
|
|
40
|
+
status: this.status
|
|
41
|
+
};
|
|
42
|
+
if (this.expose && this.details) {
|
|
43
|
+
json.details = this.details;
|
|
44
|
+
}
|
|
45
|
+
return json;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a response-safe version of the error
|
|
49
|
+
*/
|
|
50
|
+
toResponse() {
|
|
51
|
+
return {
|
|
52
|
+
status: this.status,
|
|
53
|
+
body: this.toJSON()
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var HTTP_STATUS_MESSAGES = {
|
|
58
|
+
400: "Bad Request",
|
|
59
|
+
401: "Unauthorized",
|
|
60
|
+
402: "Payment Required",
|
|
61
|
+
403: "Forbidden",
|
|
62
|
+
404: "Not Found",
|
|
63
|
+
405: "Method Not Allowed",
|
|
64
|
+
406: "Not Acceptable",
|
|
65
|
+
407: "Proxy Authentication Required",
|
|
66
|
+
408: "Request Timeout",
|
|
67
|
+
409: "Conflict",
|
|
68
|
+
410: "Gone",
|
|
69
|
+
411: "Length Required",
|
|
70
|
+
412: "Precondition Failed",
|
|
71
|
+
413: "Payload Too Large",
|
|
72
|
+
414: "URI Too Long",
|
|
73
|
+
415: "Unsupported Media Type",
|
|
74
|
+
416: "Range Not Satisfiable",
|
|
75
|
+
417: "Expectation Failed",
|
|
76
|
+
418: "I'm a Teapot",
|
|
77
|
+
422: "Unprocessable Entity",
|
|
78
|
+
423: "Locked",
|
|
79
|
+
424: "Failed Dependency",
|
|
80
|
+
425: "Too Early",
|
|
81
|
+
426: "Upgrade Required",
|
|
82
|
+
428: "Precondition Required",
|
|
83
|
+
429: "Too Many Requests",
|
|
84
|
+
431: "Request Header Fields Too Large",
|
|
85
|
+
451: "Unavailable For Legal Reasons",
|
|
86
|
+
500: "Internal Server Error",
|
|
87
|
+
501: "Not Implemented",
|
|
88
|
+
502: "Bad Gateway",
|
|
89
|
+
503: "Service Unavailable",
|
|
90
|
+
504: "Gateway Timeout",
|
|
91
|
+
505: "HTTP Version Not Supported",
|
|
92
|
+
506: "Variant Also Negotiates",
|
|
93
|
+
507: "Insufficient Storage",
|
|
94
|
+
508: "Loop Detected",
|
|
95
|
+
510: "Not Extended",
|
|
96
|
+
511: "Network Authentication Required"
|
|
97
|
+
};
|
|
98
|
+
function getHttpStatusMessage(status) {
|
|
99
|
+
return HTTP_STATUS_MESSAGES[status] ?? `HTTP Error ${status}`;
|
|
100
|
+
}
|
|
101
|
+
__name(getHttpStatusMessage, "getHttpStatusMessage");
|
|
102
|
+
var HttpError = class extends NextRushError {
|
|
103
|
+
static {
|
|
104
|
+
__name(this, "HttpError");
|
|
105
|
+
}
|
|
106
|
+
constructor(status, message, options = {}) {
|
|
107
|
+
super(message ?? getHttpStatusMessage(status), {
|
|
108
|
+
status,
|
|
109
|
+
code: options.code ?? `HTTP_${status}`,
|
|
110
|
+
expose: options.expose ?? status < 500,
|
|
111
|
+
details: options.details,
|
|
112
|
+
cause: options.cause
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/http-errors.ts
|
|
118
|
+
var BadRequestError = class extends HttpError {
|
|
119
|
+
static {
|
|
120
|
+
__name(this, "BadRequestError");
|
|
121
|
+
}
|
|
122
|
+
constructor(message = "Bad Request", options = {}) {
|
|
123
|
+
super(400, message, {
|
|
124
|
+
code: "BAD_REQUEST",
|
|
125
|
+
...options
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var UnauthorizedError = class extends HttpError {
|
|
130
|
+
static {
|
|
131
|
+
__name(this, "UnauthorizedError");
|
|
132
|
+
}
|
|
133
|
+
constructor(message = "Unauthorized", options = {}) {
|
|
134
|
+
super(401, message, {
|
|
135
|
+
code: "UNAUTHORIZED",
|
|
136
|
+
...options
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var PaymentRequiredError = class extends HttpError {
|
|
141
|
+
static {
|
|
142
|
+
__name(this, "PaymentRequiredError");
|
|
143
|
+
}
|
|
144
|
+
constructor(message = "Payment Required", options = {}) {
|
|
145
|
+
super(402, message, {
|
|
146
|
+
code: "PAYMENT_REQUIRED",
|
|
147
|
+
...options
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
var ForbiddenError = class extends HttpError {
|
|
152
|
+
static {
|
|
153
|
+
__name(this, "ForbiddenError");
|
|
154
|
+
}
|
|
155
|
+
constructor(message = "Forbidden", options = {}) {
|
|
156
|
+
super(403, message, {
|
|
157
|
+
code: "FORBIDDEN",
|
|
158
|
+
...options
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
var NotFoundError = class extends HttpError {
|
|
163
|
+
static {
|
|
164
|
+
__name(this, "NotFoundError");
|
|
165
|
+
}
|
|
166
|
+
constructor(message = "Not Found", options = {}) {
|
|
167
|
+
super(404, message, {
|
|
168
|
+
code: "NOT_FOUND",
|
|
169
|
+
...options
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var MethodNotAllowedError = class extends HttpError {
|
|
174
|
+
static {
|
|
175
|
+
__name(this, "MethodNotAllowedError");
|
|
176
|
+
}
|
|
177
|
+
allowedMethods;
|
|
178
|
+
constructor(allowedMethods = [], message = "Method Not Allowed", options = {}) {
|
|
179
|
+
super(405, message, {
|
|
180
|
+
code: "METHOD_NOT_ALLOWED",
|
|
181
|
+
details: {
|
|
182
|
+
allowedMethods
|
|
183
|
+
},
|
|
184
|
+
...options
|
|
185
|
+
});
|
|
186
|
+
this.allowedMethods = allowedMethods;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
var NotAcceptableError = class extends HttpError {
|
|
190
|
+
static {
|
|
191
|
+
__name(this, "NotAcceptableError");
|
|
192
|
+
}
|
|
193
|
+
constructor(message = "Not Acceptable", options = {}) {
|
|
194
|
+
super(406, message, {
|
|
195
|
+
code: "NOT_ACCEPTABLE",
|
|
196
|
+
...options
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
var ProxyAuthRequiredError = class extends HttpError {
|
|
201
|
+
static {
|
|
202
|
+
__name(this, "ProxyAuthRequiredError");
|
|
203
|
+
}
|
|
204
|
+
constructor(message = "Proxy Authentication Required", options = {}) {
|
|
205
|
+
super(407, message, {
|
|
206
|
+
code: "PROXY_AUTH_REQUIRED",
|
|
207
|
+
...options
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
var RequestTimeoutError = class extends HttpError {
|
|
212
|
+
static {
|
|
213
|
+
__name(this, "RequestTimeoutError");
|
|
214
|
+
}
|
|
215
|
+
constructor(message = "Request Timeout", options = {}) {
|
|
216
|
+
super(408, message, {
|
|
217
|
+
code: "REQUEST_TIMEOUT",
|
|
218
|
+
...options
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var ConflictError = class extends HttpError {
|
|
223
|
+
static {
|
|
224
|
+
__name(this, "ConflictError");
|
|
225
|
+
}
|
|
226
|
+
constructor(message = "Conflict", options = {}) {
|
|
227
|
+
super(409, message, {
|
|
228
|
+
code: "CONFLICT",
|
|
229
|
+
...options
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
var GoneError = class extends HttpError {
|
|
234
|
+
static {
|
|
235
|
+
__name(this, "GoneError");
|
|
236
|
+
}
|
|
237
|
+
constructor(message = "Gone", options = {}) {
|
|
238
|
+
super(410, message, {
|
|
239
|
+
code: "GONE",
|
|
240
|
+
...options
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
var LengthRequiredError = class extends HttpError {
|
|
245
|
+
static {
|
|
246
|
+
__name(this, "LengthRequiredError");
|
|
247
|
+
}
|
|
248
|
+
constructor(message = "Length Required", options = {}) {
|
|
249
|
+
super(411, message, {
|
|
250
|
+
code: "LENGTH_REQUIRED",
|
|
251
|
+
...options
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
var PreconditionFailedError = class extends HttpError {
|
|
256
|
+
static {
|
|
257
|
+
__name(this, "PreconditionFailedError");
|
|
258
|
+
}
|
|
259
|
+
constructor(message = "Precondition Failed", options = {}) {
|
|
260
|
+
super(412, message, {
|
|
261
|
+
code: "PRECONDITION_FAILED",
|
|
262
|
+
...options
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
var PayloadTooLargeError = class extends HttpError {
|
|
267
|
+
static {
|
|
268
|
+
__name(this, "PayloadTooLargeError");
|
|
269
|
+
}
|
|
270
|
+
constructor(message = "Payload Too Large", options = {}) {
|
|
271
|
+
super(413, message, {
|
|
272
|
+
code: "PAYLOAD_TOO_LARGE",
|
|
273
|
+
...options
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
var UriTooLongError = class extends HttpError {
|
|
278
|
+
static {
|
|
279
|
+
__name(this, "UriTooLongError");
|
|
280
|
+
}
|
|
281
|
+
constructor(message = "URI Too Long", options = {}) {
|
|
282
|
+
super(414, message, {
|
|
283
|
+
code: "URI_TOO_LONG",
|
|
284
|
+
...options
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
var UnsupportedMediaTypeError = class extends HttpError {
|
|
289
|
+
static {
|
|
290
|
+
__name(this, "UnsupportedMediaTypeError");
|
|
291
|
+
}
|
|
292
|
+
constructor(message = "Unsupported Media Type", options = {}) {
|
|
293
|
+
super(415, message, {
|
|
294
|
+
code: "UNSUPPORTED_MEDIA_TYPE",
|
|
295
|
+
...options
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
var RangeNotSatisfiableError = class extends HttpError {
|
|
300
|
+
static {
|
|
301
|
+
__name(this, "RangeNotSatisfiableError");
|
|
302
|
+
}
|
|
303
|
+
constructor(message = "Range Not Satisfiable", options = {}) {
|
|
304
|
+
super(416, message, {
|
|
305
|
+
code: "RANGE_NOT_SATISFIABLE",
|
|
306
|
+
...options
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
var ExpectationFailedError = class extends HttpError {
|
|
311
|
+
static {
|
|
312
|
+
__name(this, "ExpectationFailedError");
|
|
313
|
+
}
|
|
314
|
+
constructor(message = "Expectation Failed", options = {}) {
|
|
315
|
+
super(417, message, {
|
|
316
|
+
code: "EXPECTATION_FAILED",
|
|
317
|
+
...options
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
var ImATeapotError = class extends HttpError {
|
|
322
|
+
static {
|
|
323
|
+
__name(this, "ImATeapotError");
|
|
324
|
+
}
|
|
325
|
+
constructor(message = "I'm a teapot", options = {}) {
|
|
326
|
+
super(418, message, {
|
|
327
|
+
code: "IM_A_TEAPOT",
|
|
328
|
+
...options
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
var UnprocessableEntityError = class extends HttpError {
|
|
333
|
+
static {
|
|
334
|
+
__name(this, "UnprocessableEntityError");
|
|
335
|
+
}
|
|
336
|
+
constructor(message = "Unprocessable Entity", options = {}) {
|
|
337
|
+
super(422, message, {
|
|
338
|
+
code: "UNPROCESSABLE_ENTITY",
|
|
339
|
+
...options
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
var LockedError = class extends HttpError {
|
|
344
|
+
static {
|
|
345
|
+
__name(this, "LockedError");
|
|
346
|
+
}
|
|
347
|
+
constructor(message = "Locked", options = {}) {
|
|
348
|
+
super(423, message, {
|
|
349
|
+
code: "LOCKED",
|
|
350
|
+
...options
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
var FailedDependencyError = class extends HttpError {
|
|
355
|
+
static {
|
|
356
|
+
__name(this, "FailedDependencyError");
|
|
357
|
+
}
|
|
358
|
+
constructor(message = "Failed Dependency", options = {}) {
|
|
359
|
+
super(424, message, {
|
|
360
|
+
code: "FAILED_DEPENDENCY",
|
|
361
|
+
...options
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
var TooEarlyError = class extends HttpError {
|
|
366
|
+
static {
|
|
367
|
+
__name(this, "TooEarlyError");
|
|
368
|
+
}
|
|
369
|
+
constructor(message = "Too Early", options = {}) {
|
|
370
|
+
super(425, message, {
|
|
371
|
+
code: "TOO_EARLY",
|
|
372
|
+
...options
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
var UpgradeRequiredError = class extends HttpError {
|
|
377
|
+
static {
|
|
378
|
+
__name(this, "UpgradeRequiredError");
|
|
379
|
+
}
|
|
380
|
+
constructor(message = "Upgrade Required", options = {}) {
|
|
381
|
+
super(426, message, {
|
|
382
|
+
code: "UPGRADE_REQUIRED",
|
|
383
|
+
...options
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
var PreconditionRequiredError = class extends HttpError {
|
|
388
|
+
static {
|
|
389
|
+
__name(this, "PreconditionRequiredError");
|
|
390
|
+
}
|
|
391
|
+
constructor(message = "Precondition Required", options = {}) {
|
|
392
|
+
super(428, message, {
|
|
393
|
+
code: "PRECONDITION_REQUIRED",
|
|
394
|
+
...options
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
var TooManyRequestsError = class extends HttpError {
|
|
399
|
+
static {
|
|
400
|
+
__name(this, "TooManyRequestsError");
|
|
401
|
+
}
|
|
402
|
+
retryAfter;
|
|
403
|
+
constructor(message = "Too Many Requests", options = {}) {
|
|
404
|
+
super(429, message, {
|
|
405
|
+
code: "TOO_MANY_REQUESTS",
|
|
406
|
+
details: options.retryAfter ? {
|
|
407
|
+
retryAfter: options.retryAfter
|
|
408
|
+
} : void 0,
|
|
409
|
+
...options
|
|
410
|
+
});
|
|
411
|
+
this.retryAfter = options.retryAfter;
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
var RequestHeaderFieldsTooLargeError = class extends HttpError {
|
|
415
|
+
static {
|
|
416
|
+
__name(this, "RequestHeaderFieldsTooLargeError");
|
|
417
|
+
}
|
|
418
|
+
constructor(message = "Request Header Fields Too Large", options = {}) {
|
|
419
|
+
super(431, message, {
|
|
420
|
+
code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
|
|
421
|
+
...options
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
var UnavailableForLegalReasonsError = class extends HttpError {
|
|
426
|
+
static {
|
|
427
|
+
__name(this, "UnavailableForLegalReasonsError");
|
|
428
|
+
}
|
|
429
|
+
constructor(message = "Unavailable For Legal Reasons", options = {}) {
|
|
430
|
+
super(451, message, {
|
|
431
|
+
code: "UNAVAILABLE_FOR_LEGAL_REASONS",
|
|
432
|
+
...options
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
var InternalServerError = class extends HttpError {
|
|
437
|
+
static {
|
|
438
|
+
__name(this, "InternalServerError");
|
|
439
|
+
}
|
|
440
|
+
constructor(message = "Internal Server Error", options = {}) {
|
|
441
|
+
super(500, message, {
|
|
442
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
443
|
+
expose: false,
|
|
444
|
+
...options
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
var NotImplementedError = class extends HttpError {
|
|
449
|
+
static {
|
|
450
|
+
__name(this, "NotImplementedError");
|
|
451
|
+
}
|
|
452
|
+
constructor(message = "Not Implemented", options = {}) {
|
|
453
|
+
super(501, message, {
|
|
454
|
+
code: "NOT_IMPLEMENTED",
|
|
455
|
+
expose: false,
|
|
456
|
+
...options
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
var BadGatewayError = class extends HttpError {
|
|
461
|
+
static {
|
|
462
|
+
__name(this, "BadGatewayError");
|
|
463
|
+
}
|
|
464
|
+
constructor(message = "Bad Gateway", options = {}) {
|
|
465
|
+
super(502, message, {
|
|
466
|
+
code: "BAD_GATEWAY",
|
|
467
|
+
expose: false,
|
|
468
|
+
...options
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
var ServiceUnavailableError = class extends HttpError {
|
|
473
|
+
static {
|
|
474
|
+
__name(this, "ServiceUnavailableError");
|
|
475
|
+
}
|
|
476
|
+
retryAfter;
|
|
477
|
+
constructor(message = "Service Unavailable", options = {}) {
|
|
478
|
+
super(503, message, {
|
|
479
|
+
code: "SERVICE_UNAVAILABLE",
|
|
480
|
+
expose: false,
|
|
481
|
+
...options
|
|
482
|
+
});
|
|
483
|
+
this.retryAfter = options.retryAfter;
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
var GatewayTimeoutError = class extends HttpError {
|
|
487
|
+
static {
|
|
488
|
+
__name(this, "GatewayTimeoutError");
|
|
489
|
+
}
|
|
490
|
+
constructor(message = "Gateway Timeout", options = {}) {
|
|
491
|
+
super(504, message, {
|
|
492
|
+
code: "GATEWAY_TIMEOUT",
|
|
493
|
+
expose: false,
|
|
494
|
+
...options
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
var HttpVersionNotSupportedError = class extends HttpError {
|
|
499
|
+
static {
|
|
500
|
+
__name(this, "HttpVersionNotSupportedError");
|
|
501
|
+
}
|
|
502
|
+
constructor(message = "HTTP Version Not Supported", options = {}) {
|
|
503
|
+
super(505, message, {
|
|
504
|
+
code: "HTTP_VERSION_NOT_SUPPORTED",
|
|
505
|
+
expose: false,
|
|
506
|
+
...options
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
var VariantAlsoNegotiatesError = class extends HttpError {
|
|
511
|
+
static {
|
|
512
|
+
__name(this, "VariantAlsoNegotiatesError");
|
|
513
|
+
}
|
|
514
|
+
constructor(message = "Variant Also Negotiates", options = {}) {
|
|
515
|
+
super(506, message, {
|
|
516
|
+
code: "VARIANT_ALSO_NEGOTIATES",
|
|
517
|
+
expose: false,
|
|
518
|
+
...options
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
var InsufficientStorageError = class extends HttpError {
|
|
523
|
+
static {
|
|
524
|
+
__name(this, "InsufficientStorageError");
|
|
525
|
+
}
|
|
526
|
+
constructor(message = "Insufficient Storage", options = {}) {
|
|
527
|
+
super(507, message, {
|
|
528
|
+
code: "INSUFFICIENT_STORAGE",
|
|
529
|
+
expose: false,
|
|
530
|
+
...options
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
var LoopDetectedError = class extends HttpError {
|
|
535
|
+
static {
|
|
536
|
+
__name(this, "LoopDetectedError");
|
|
537
|
+
}
|
|
538
|
+
constructor(message = "Loop Detected", options = {}) {
|
|
539
|
+
super(508, message, {
|
|
540
|
+
code: "LOOP_DETECTED",
|
|
541
|
+
expose: false,
|
|
542
|
+
...options
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
var NotExtendedError = class extends HttpError {
|
|
547
|
+
static {
|
|
548
|
+
__name(this, "NotExtendedError");
|
|
549
|
+
}
|
|
550
|
+
constructor(message = "Not Extended", options = {}) {
|
|
551
|
+
super(510, message, {
|
|
552
|
+
code: "NOT_EXTENDED",
|
|
553
|
+
expose: false,
|
|
554
|
+
...options
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
var NetworkAuthRequiredError = class extends HttpError {
|
|
559
|
+
static {
|
|
560
|
+
__name(this, "NetworkAuthRequiredError");
|
|
561
|
+
}
|
|
562
|
+
constructor(message = "Network Authentication Required", options = {}) {
|
|
563
|
+
super(511, message, {
|
|
564
|
+
code: "NETWORK_AUTH_REQUIRED",
|
|
565
|
+
expose: false,
|
|
566
|
+
...options
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
// src/validation.ts
|
|
572
|
+
var ValidationError = class _ValidationError extends NextRushError {
|
|
573
|
+
static {
|
|
574
|
+
__name(this, "ValidationError");
|
|
575
|
+
}
|
|
576
|
+
/** List of validation issues */
|
|
577
|
+
issues;
|
|
578
|
+
constructor(issues, message = "Validation failed") {
|
|
579
|
+
super(message, {
|
|
580
|
+
status: 400,
|
|
581
|
+
code: "VALIDATION_ERROR",
|
|
582
|
+
expose: true
|
|
583
|
+
});
|
|
584
|
+
this.issues = issues;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Create from a single field error
|
|
588
|
+
*/
|
|
589
|
+
static fromField(path, message, rule) {
|
|
590
|
+
return new _ValidationError([
|
|
591
|
+
{
|
|
592
|
+
path,
|
|
593
|
+
message,
|
|
594
|
+
rule
|
|
595
|
+
}
|
|
596
|
+
]);
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Create from multiple field errors
|
|
600
|
+
*/
|
|
601
|
+
static fromFields(errors) {
|
|
602
|
+
const issues = Object.entries(errors).map(([path, message]) => ({
|
|
603
|
+
path,
|
|
604
|
+
message
|
|
605
|
+
}));
|
|
606
|
+
return new _ValidationError(issues);
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Check if a specific field has errors
|
|
610
|
+
*/
|
|
611
|
+
hasErrorFor(path) {
|
|
612
|
+
return this.issues.some((issue) => issue.path === path);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Get errors for a specific field
|
|
616
|
+
*/
|
|
617
|
+
getErrorsFor(path) {
|
|
618
|
+
return this.issues.filter((issue) => issue.path === path);
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Get first error message for a field
|
|
622
|
+
*/
|
|
623
|
+
getFirstError(path) {
|
|
624
|
+
return this.issues.find((issue) => issue.path === path)?.message;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Convert to flat error object
|
|
628
|
+
*/
|
|
629
|
+
toFlatObject() {
|
|
630
|
+
const result = {};
|
|
631
|
+
for (const issue of this.issues) {
|
|
632
|
+
if (!result[issue.path]) {
|
|
633
|
+
result[issue.path] = issue.message;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return result;
|
|
637
|
+
}
|
|
638
|
+
toJSON() {
|
|
639
|
+
return {
|
|
640
|
+
error: this.name,
|
|
641
|
+
message: this.message,
|
|
642
|
+
code: this.code,
|
|
643
|
+
status: this.status,
|
|
644
|
+
// Strip `received` to prevent leaking sensitive input values (passwords, tokens)
|
|
645
|
+
issues: this.issues.map(({ path, message, rule, expected }) => ({
|
|
646
|
+
path,
|
|
647
|
+
message,
|
|
648
|
+
...rule !== void 0 && {
|
|
649
|
+
rule
|
|
650
|
+
},
|
|
651
|
+
...expected !== void 0 && {
|
|
652
|
+
expected
|
|
653
|
+
}
|
|
654
|
+
}))
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
var RequiredFieldError = class extends ValidationError {
|
|
659
|
+
static {
|
|
660
|
+
__name(this, "RequiredFieldError");
|
|
661
|
+
}
|
|
662
|
+
constructor(field) {
|
|
663
|
+
super([
|
|
664
|
+
{
|
|
665
|
+
path: field,
|
|
666
|
+
message: `${field} is required`,
|
|
667
|
+
rule: "required"
|
|
668
|
+
}
|
|
669
|
+
], `${field} is required`);
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
var TypeMismatchError = class extends ValidationError {
|
|
673
|
+
static {
|
|
674
|
+
__name(this, "TypeMismatchError");
|
|
675
|
+
}
|
|
676
|
+
constructor(field, expected, received) {
|
|
677
|
+
super([
|
|
678
|
+
{
|
|
679
|
+
path: field,
|
|
680
|
+
message: `Expected ${expected}, received ${received}`,
|
|
681
|
+
rule: "type",
|
|
682
|
+
expected,
|
|
683
|
+
received
|
|
684
|
+
}
|
|
685
|
+
], `${field} must be of type ${expected}`);
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
var RangeValidationError = class extends ValidationError {
|
|
689
|
+
static {
|
|
690
|
+
__name(this, "RangeValidationError");
|
|
691
|
+
}
|
|
692
|
+
constructor(field, min, max) {
|
|
693
|
+
const parts = [];
|
|
694
|
+
if (min !== void 0) parts.push(`at least ${min}`);
|
|
695
|
+
if (max !== void 0) parts.push(`at most ${max}`);
|
|
696
|
+
const message = `${field} must be ${parts.join(" and ")}`;
|
|
697
|
+
super([
|
|
698
|
+
{
|
|
699
|
+
path: field,
|
|
700
|
+
message,
|
|
701
|
+
rule: "range",
|
|
702
|
+
expected: {
|
|
703
|
+
min,
|
|
704
|
+
max
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
], message);
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
var LengthError = class extends ValidationError {
|
|
711
|
+
static {
|
|
712
|
+
__name(this, "LengthError");
|
|
713
|
+
}
|
|
714
|
+
constructor(field, min, max) {
|
|
715
|
+
const parts = [];
|
|
716
|
+
if (min !== void 0) parts.push(`at least ${min} characters`);
|
|
717
|
+
if (max !== void 0) parts.push(`at most ${max} characters`);
|
|
718
|
+
const message = `${field} must be ${parts.join(" and ")}`;
|
|
719
|
+
super([
|
|
720
|
+
{
|
|
721
|
+
path: field,
|
|
722
|
+
message,
|
|
723
|
+
rule: "length",
|
|
724
|
+
expected: {
|
|
725
|
+
min,
|
|
726
|
+
max
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
], message);
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
var PatternError = class extends ValidationError {
|
|
733
|
+
static {
|
|
734
|
+
__name(this, "PatternError");
|
|
735
|
+
}
|
|
736
|
+
constructor(field, pattern, message) {
|
|
737
|
+
super([
|
|
738
|
+
{
|
|
739
|
+
path: field,
|
|
740
|
+
message: message ?? `${field} does not match required pattern`,
|
|
741
|
+
rule: "pattern",
|
|
742
|
+
expected: pattern
|
|
743
|
+
}
|
|
744
|
+
], message ?? `${field} does not match required pattern`);
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
var InvalidEmailError = class extends ValidationError {
|
|
748
|
+
static {
|
|
749
|
+
__name(this, "InvalidEmailError");
|
|
750
|
+
}
|
|
751
|
+
constructor(field = "email") {
|
|
752
|
+
super([
|
|
753
|
+
{
|
|
754
|
+
path: field,
|
|
755
|
+
message: "Invalid email address",
|
|
756
|
+
rule: "email"
|
|
757
|
+
}
|
|
758
|
+
], "Invalid email address");
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
var InvalidUrlError = class extends ValidationError {
|
|
762
|
+
static {
|
|
763
|
+
__name(this, "InvalidUrlError");
|
|
764
|
+
}
|
|
765
|
+
constructor(field = "url") {
|
|
766
|
+
super([
|
|
767
|
+
{
|
|
768
|
+
path: field,
|
|
769
|
+
message: "Invalid URL",
|
|
770
|
+
rule: "url"
|
|
771
|
+
}
|
|
772
|
+
], "Invalid URL");
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
// src/factory.ts
|
|
777
|
+
var ERROR_MAP = {
|
|
778
|
+
400: BadRequestError,
|
|
779
|
+
401: UnauthorizedError,
|
|
780
|
+
403: ForbiddenError,
|
|
781
|
+
404: NotFoundError,
|
|
782
|
+
409: ConflictError,
|
|
783
|
+
422: UnprocessableEntityError,
|
|
784
|
+
429: TooManyRequestsError,
|
|
785
|
+
500: InternalServerError,
|
|
786
|
+
501: NotImplementedError,
|
|
787
|
+
502: BadGatewayError,
|
|
788
|
+
503: ServiceUnavailableError,
|
|
789
|
+
504: GatewayTimeoutError
|
|
790
|
+
};
|
|
791
|
+
function createError(status, message, options) {
|
|
792
|
+
if (status === 405) {
|
|
793
|
+
return new MethodNotAllowedError([], message, options);
|
|
794
|
+
}
|
|
795
|
+
const ErrorClass = ERROR_MAP[status];
|
|
796
|
+
if (ErrorClass) {
|
|
797
|
+
return new ErrorClass(message, options);
|
|
798
|
+
}
|
|
799
|
+
return new HttpError(status, message, options);
|
|
800
|
+
}
|
|
801
|
+
__name(createError, "createError");
|
|
802
|
+
function badRequest(message, options) {
|
|
803
|
+
return new BadRequestError(message, options);
|
|
804
|
+
}
|
|
805
|
+
__name(badRequest, "badRequest");
|
|
806
|
+
function unauthorized(message, options) {
|
|
807
|
+
return new UnauthorizedError(message, options);
|
|
808
|
+
}
|
|
809
|
+
__name(unauthorized, "unauthorized");
|
|
810
|
+
function forbidden(message, options) {
|
|
811
|
+
return new ForbiddenError(message, options);
|
|
812
|
+
}
|
|
813
|
+
__name(forbidden, "forbidden");
|
|
814
|
+
function notFound(message, options) {
|
|
815
|
+
return new NotFoundError(message, options);
|
|
816
|
+
}
|
|
817
|
+
__name(notFound, "notFound");
|
|
818
|
+
function methodNotAllowed(allowedMethods, message, options) {
|
|
819
|
+
return new MethodNotAllowedError(allowedMethods, message, options);
|
|
820
|
+
}
|
|
821
|
+
__name(methodNotAllowed, "methodNotAllowed");
|
|
822
|
+
function conflict(message, options) {
|
|
823
|
+
return new ConflictError(message, options);
|
|
824
|
+
}
|
|
825
|
+
__name(conflict, "conflict");
|
|
826
|
+
function unprocessableEntity(message, options) {
|
|
827
|
+
return new UnprocessableEntityError(message, options);
|
|
828
|
+
}
|
|
829
|
+
__name(unprocessableEntity, "unprocessableEntity");
|
|
830
|
+
function tooManyRequests(message, options) {
|
|
831
|
+
return new TooManyRequestsError(message, options);
|
|
832
|
+
}
|
|
833
|
+
__name(tooManyRequests, "tooManyRequests");
|
|
834
|
+
function internalError(message, options) {
|
|
835
|
+
return new InternalServerError(message, options);
|
|
836
|
+
}
|
|
837
|
+
__name(internalError, "internalError");
|
|
838
|
+
function badGateway(message, options) {
|
|
839
|
+
return new BadGatewayError(message, options);
|
|
840
|
+
}
|
|
841
|
+
__name(badGateway, "badGateway");
|
|
842
|
+
function serviceUnavailable(message, options) {
|
|
843
|
+
return new ServiceUnavailableError(message, options);
|
|
844
|
+
}
|
|
845
|
+
__name(serviceUnavailable, "serviceUnavailable");
|
|
846
|
+
function gatewayTimeout(message, options) {
|
|
847
|
+
return new GatewayTimeoutError(message, options);
|
|
848
|
+
}
|
|
849
|
+
__name(gatewayTimeout, "gatewayTimeout");
|
|
850
|
+
function isHttpError(error) {
|
|
851
|
+
return error instanceof HttpError;
|
|
852
|
+
}
|
|
853
|
+
__name(isHttpError, "isHttpError");
|
|
854
|
+
function getErrorStatus(error) {
|
|
855
|
+
if (error instanceof NextRushError) {
|
|
856
|
+
return error.status;
|
|
857
|
+
}
|
|
858
|
+
if (error != null && typeof error === "object" && "status" in error && typeof error.status === "number") {
|
|
859
|
+
const status = error.status;
|
|
860
|
+
return status >= 400 && status < 600 ? status : 500;
|
|
861
|
+
}
|
|
862
|
+
return 500;
|
|
863
|
+
}
|
|
864
|
+
__name(getErrorStatus, "getErrorStatus");
|
|
865
|
+
function getSafeErrorMessage(error) {
|
|
866
|
+
if (error instanceof NextRushError && error.expose) {
|
|
867
|
+
return error.message;
|
|
868
|
+
}
|
|
869
|
+
return "Internal Server Error";
|
|
870
|
+
}
|
|
871
|
+
__name(getSafeErrorMessage, "getSafeErrorMessage");
|
|
872
|
+
|
|
873
|
+
// src/middleware.ts
|
|
874
|
+
function defaultLogger(error, ctx) {
|
|
875
|
+
const status = error instanceof HttpError ? error.status : 500;
|
|
876
|
+
if (status >= 500) {
|
|
877
|
+
console.error(`[ERROR] ${ctx.method} ${ctx.path}:`, error);
|
|
878
|
+
} else {
|
|
879
|
+
console.warn(`[WARN] ${ctx.method} ${ctx.path}: ${error.message}`);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
__name(defaultLogger, "defaultLogger");
|
|
883
|
+
function errorHandler(options = {}) {
|
|
884
|
+
const { includeStack = false, logger = defaultLogger, transform, handlers } = options;
|
|
885
|
+
return async (ctx, next) => {
|
|
886
|
+
try {
|
|
887
|
+
await next();
|
|
888
|
+
} catch (error) {
|
|
889
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
890
|
+
logger(err, ctx);
|
|
891
|
+
if (handlers) {
|
|
892
|
+
for (const [ErrorType, handler] of handlers) {
|
|
893
|
+
if (err instanceof ErrorType) {
|
|
894
|
+
handler(err, ctx);
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
let status = 500;
|
|
900
|
+
let expose = false;
|
|
901
|
+
let code = "INTERNAL_ERROR";
|
|
902
|
+
let details;
|
|
903
|
+
if (err instanceof HttpError || err instanceof NextRushError) {
|
|
904
|
+
status = err.status;
|
|
905
|
+
expose = err.expose;
|
|
906
|
+
code = err.code;
|
|
907
|
+
details = err.details;
|
|
908
|
+
}
|
|
909
|
+
ctx.status = status;
|
|
910
|
+
let body;
|
|
911
|
+
if (transform) {
|
|
912
|
+
body = transform(err, ctx);
|
|
913
|
+
} else {
|
|
914
|
+
body = {
|
|
915
|
+
error: expose ? err.name : getHttpStatusMessage(status),
|
|
916
|
+
message: expose ? err.message : "Internal Server Error",
|
|
917
|
+
code,
|
|
918
|
+
status
|
|
919
|
+
};
|
|
920
|
+
if (expose && details) {
|
|
921
|
+
body.details = details;
|
|
922
|
+
}
|
|
923
|
+
if (includeStack && err.stack) {
|
|
924
|
+
body.stack = err.stack.split("\n").map((line) => line.trim());
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
ctx.json(body);
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
__name(errorHandler, "errorHandler");
|
|
932
|
+
function notFoundHandler(message = "Not Found") {
|
|
933
|
+
return async (ctx, next) => {
|
|
934
|
+
await next();
|
|
935
|
+
if (!ctx.responded && ctx.status === 404) {
|
|
936
|
+
ctx.json({
|
|
937
|
+
error: "NotFoundError",
|
|
938
|
+
message,
|
|
939
|
+
code: "NOT_FOUND",
|
|
940
|
+
status: 404
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
__name(notFoundHandler, "notFoundHandler");
|
|
946
|
+
function catchAsync(handler) {
|
|
947
|
+
return handler;
|
|
948
|
+
}
|
|
949
|
+
__name(catchAsync, "catchAsync");
|
|
950
|
+
export {
|
|
951
|
+
BadGatewayError,
|
|
952
|
+
BadRequestError,
|
|
953
|
+
ConflictError,
|
|
954
|
+
ExpectationFailedError,
|
|
955
|
+
FailedDependencyError,
|
|
956
|
+
ForbiddenError,
|
|
957
|
+
GatewayTimeoutError,
|
|
958
|
+
GoneError,
|
|
959
|
+
HttpError,
|
|
960
|
+
HttpVersionNotSupportedError,
|
|
961
|
+
ImATeapotError,
|
|
962
|
+
InsufficientStorageError,
|
|
963
|
+
InternalServerError,
|
|
964
|
+
InvalidEmailError,
|
|
965
|
+
InvalidUrlError,
|
|
966
|
+
LengthError,
|
|
967
|
+
LengthRequiredError,
|
|
968
|
+
LockedError,
|
|
969
|
+
LoopDetectedError,
|
|
970
|
+
MethodNotAllowedError,
|
|
971
|
+
NetworkAuthRequiredError,
|
|
972
|
+
NextRushError,
|
|
973
|
+
NotAcceptableError,
|
|
974
|
+
NotExtendedError,
|
|
975
|
+
NotFoundError,
|
|
976
|
+
NotImplementedError,
|
|
977
|
+
PatternError,
|
|
978
|
+
PayloadTooLargeError,
|
|
979
|
+
PaymentRequiredError,
|
|
980
|
+
PreconditionFailedError,
|
|
981
|
+
PreconditionRequiredError,
|
|
982
|
+
ProxyAuthRequiredError,
|
|
983
|
+
RangeNotSatisfiableError,
|
|
984
|
+
RangeValidationError,
|
|
985
|
+
RequestHeaderFieldsTooLargeError,
|
|
986
|
+
RequestTimeoutError,
|
|
987
|
+
RequiredFieldError,
|
|
988
|
+
ServiceUnavailableError,
|
|
989
|
+
TooEarlyError,
|
|
990
|
+
TooManyRequestsError,
|
|
991
|
+
TypeMismatchError,
|
|
992
|
+
UnauthorizedError,
|
|
993
|
+
UnavailableForLegalReasonsError,
|
|
994
|
+
UnprocessableEntityError,
|
|
995
|
+
UnsupportedMediaTypeError,
|
|
996
|
+
UpgradeRequiredError,
|
|
997
|
+
UriTooLongError,
|
|
998
|
+
ValidationError,
|
|
999
|
+
VariantAlsoNegotiatesError,
|
|
1000
|
+
badGateway,
|
|
1001
|
+
badRequest,
|
|
1002
|
+
catchAsync,
|
|
1003
|
+
conflict,
|
|
1004
|
+
createError,
|
|
1005
|
+
errorHandler,
|
|
1006
|
+
forbidden,
|
|
1007
|
+
gatewayTimeout,
|
|
1008
|
+
getErrorStatus,
|
|
1009
|
+
getHttpStatusMessage,
|
|
1010
|
+
getSafeErrorMessage,
|
|
1011
|
+
internalError,
|
|
1012
|
+
isHttpError,
|
|
1013
|
+
methodNotAllowed,
|
|
1014
|
+
notFound,
|
|
1015
|
+
notFoundHandler,
|
|
1016
|
+
serviceUnavailable,
|
|
1017
|
+
tooManyRequests,
|
|
1018
|
+
unauthorized,
|
|
1019
|
+
unprocessableEntity
|
|
1020
|
+
};
|
|
1021
|
+
//# sourceMappingURL=index.js.map
|