@bombillazo/error-x 0.4.6 → 0.5.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/README.md +479 -195
- package/dist/index.cjs +563 -233
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1067 -429
- package/dist/index.d.ts +1067 -429
- package/dist/index.js +561 -233
- package/dist/index.js.map +1 -1
- package/package.json +2 -15
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var deepmergeTs = require('deepmerge-ts');
|
|
3
4
|
var safeStringify = require('safe-stringify');
|
|
4
5
|
|
|
5
6
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -16,9 +17,7 @@ var ERROR_X_OPTION_FIELDS = [
|
|
|
16
17
|
"uiMessage",
|
|
17
18
|
"cause",
|
|
18
19
|
"metadata",
|
|
19
|
-
"
|
|
20
|
-
"docsUrl",
|
|
21
|
-
"source"
|
|
20
|
+
"httpStatus"
|
|
22
21
|
];
|
|
23
22
|
|
|
24
23
|
// src/error.ts
|
|
@@ -34,14 +33,33 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
34
33
|
metadata;
|
|
35
34
|
/** Unix epoch timestamp (milliseconds) when the error was created */
|
|
36
35
|
timestamp;
|
|
37
|
-
/**
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
|
|
36
|
+
/** HTTP status code associated with this error */
|
|
37
|
+
httpStatus;
|
|
38
|
+
/** Serialized non-ErrorX entity this was wrapped from (if created via ErrorX.from()) */
|
|
39
|
+
original;
|
|
40
|
+
/** Error chain timeline: [this, parent, grandparent, ...] - single source of truth */
|
|
41
|
+
_chain = [];
|
|
42
|
+
/**
|
|
43
|
+
* Gets the immediate parent ErrorX in the chain (if any).
|
|
44
|
+
* @returns The ErrorX that caused this error, or undefined if this is the root
|
|
45
|
+
*/
|
|
46
|
+
get parent() {
|
|
47
|
+
return this._chain[1];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Gets the deepest ErrorX in the chain (the original root cause).
|
|
51
|
+
* @returns The root cause ErrorX, or undefined if chain has only this error
|
|
52
|
+
*/
|
|
53
|
+
get root() {
|
|
54
|
+
return this._chain.length > 1 ? this._chain[this._chain.length - 1] : void 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets the full error chain timeline.
|
|
58
|
+
* @returns Array of ErrorX instances: [this, parent, grandparent, ...]
|
|
59
|
+
*/
|
|
60
|
+
get chain() {
|
|
61
|
+
return this._chain;
|
|
62
|
+
}
|
|
45
63
|
/**
|
|
46
64
|
* Creates a new ErrorX instance with enhanced error handling capabilities.
|
|
47
65
|
*
|
|
@@ -83,34 +101,26 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
83
101
|
} else if (messageOrOptions != null) {
|
|
84
102
|
options = messageOrOptions;
|
|
85
103
|
}
|
|
86
|
-
const envConfig = _ErrorX.getConfig();
|
|
87
104
|
const message = options.message?.trim() ? options.message : "An error occurred";
|
|
88
|
-
const convertedCause = _ErrorX.toErrorXCause(options.cause);
|
|
89
105
|
super(message);
|
|
90
|
-
this.cause = convertedCause;
|
|
91
106
|
this.name = options.name ?? _ErrorX.getDefaultName();
|
|
92
107
|
this.code = options.code != null ? String(options.code) : _ErrorX.generateDefaultCode(options.name);
|
|
93
108
|
this.uiMessage = options.uiMessage;
|
|
94
109
|
this.metadata = options.metadata;
|
|
95
|
-
this.type = _ErrorX.validateType(options.type);
|
|
96
110
|
this.timestamp = Date.now();
|
|
97
|
-
this.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
generatedDocsUrl = `${base}/${path}`;
|
|
111
|
+
this.httpStatus = options.httpStatus;
|
|
112
|
+
if (options.cause != null) {
|
|
113
|
+
if (options.cause instanceof _ErrorX) {
|
|
114
|
+
this._chain = [this, ...options.cause._chain];
|
|
115
|
+
} else {
|
|
116
|
+
const wrappedCause = _ErrorX.from(options.cause);
|
|
117
|
+
this._chain = [this, ...wrappedCause._chain];
|
|
105
118
|
}
|
|
106
|
-
}
|
|
107
|
-
this.docsUrl = options.docsUrl ?? generatedDocsUrl;
|
|
108
|
-
if (convertedCause?.stack) {
|
|
109
|
-
this.stack = _ErrorX.preserveOriginalStackFromCause(convertedCause, this);
|
|
110
119
|
} else {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
this._chain = [this];
|
|
121
|
+
}
|
|
122
|
+
if (typeof Error.captureStackTrace === "function") {
|
|
123
|
+
Error.captureStackTrace(this, this.constructor);
|
|
114
124
|
}
|
|
115
125
|
this.stack = _ErrorX.cleanStack(this.stack);
|
|
116
126
|
}
|
|
@@ -168,12 +178,7 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
168
178
|
* @example
|
|
169
179
|
* ```typescript
|
|
170
180
|
* ErrorX.configure({
|
|
171
|
-
*
|
|
172
|
-
* docsBaseURL: 'https://docs.example.com/errors',
|
|
173
|
-
* docsMap: {
|
|
174
|
-
* 'AUTH_FAILED': 'authentication-errors',
|
|
175
|
-
* 'DB_ERROR': 'database-errors'
|
|
176
|
-
* },
|
|
181
|
+
* cleanStack: true, // Enable stack trace cleaning
|
|
177
182
|
* cleanStackDelimiter: 'app-entry-point' // Trim stack traces after this line
|
|
178
183
|
* })
|
|
179
184
|
* ```
|
|
@@ -201,22 +206,6 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
201
206
|
static resetConfig() {
|
|
202
207
|
_ErrorX._config = null;
|
|
203
208
|
}
|
|
204
|
-
/**
|
|
205
|
-
* Validates and normalizes the type field
|
|
206
|
-
*
|
|
207
|
-
* @param type - Type value to validate
|
|
208
|
-
* @returns Validated type string or undefined if invalid/empty
|
|
209
|
-
*/
|
|
210
|
-
static validateType(type) {
|
|
211
|
-
if (type === void 0 || type === null) {
|
|
212
|
-
return void 0;
|
|
213
|
-
}
|
|
214
|
-
const typeStr = String(type).trim();
|
|
215
|
-
if (typeStr === "") {
|
|
216
|
-
return void 0;
|
|
217
|
-
}
|
|
218
|
-
return typeStr;
|
|
219
|
-
}
|
|
220
209
|
/**
|
|
221
210
|
* Validates if an object is a valid ErrorXOptions object.
|
|
222
211
|
* Checks that the object only contains accepted ErrorXOptions fields.
|
|
@@ -256,21 +245,6 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
256
245
|
if (!name) return "ERROR";
|
|
257
246
|
return name.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/\s+/g, "_").replace(/[^a-zA-Z0-9_]/g, "").toUpperCase();
|
|
258
247
|
}
|
|
259
|
-
/**
|
|
260
|
-
* Preserves the original error's stack trace while updating the error message.
|
|
261
|
-
* Combines the new error's message with the original error's stack trace from ErrorXCause.
|
|
262
|
-
*
|
|
263
|
-
* @param cause - The ErrorXCause containing the original stack to preserve
|
|
264
|
-
* @param newError - The new error whose message to use
|
|
265
|
-
* @returns Combined stack trace with new error message and original stack
|
|
266
|
-
*/
|
|
267
|
-
static preserveOriginalStackFromCause(cause, newError) {
|
|
268
|
-
if (!cause.stack) return newError.stack || "";
|
|
269
|
-
const newErrorFirstLine = `${newError.name}: ${newError.message}`;
|
|
270
|
-
const originalStackLines = cause.stack.split("\n");
|
|
271
|
-
const originalStackTrace = originalStackLines.slice(1);
|
|
272
|
-
return [newErrorFirstLine, ...originalStackTrace].join("\n");
|
|
273
|
-
}
|
|
274
248
|
/**
|
|
275
249
|
* Cleans a stack trace by removing ErrorX internal method calls and optionally trimming after a delimiter.
|
|
276
250
|
* This provides cleaner stack traces that focus on user code.
|
|
@@ -302,7 +276,7 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
302
276
|
"new ErrorX",
|
|
303
277
|
"ErrorX.constructor",
|
|
304
278
|
"ErrorX.from",
|
|
305
|
-
"error-x/dist/",
|
|
279
|
+
"error-x/dist/error.js",
|
|
306
280
|
"error-x/src/error.ts"
|
|
307
281
|
];
|
|
308
282
|
const patterns = Array.isArray(cleanStackConfig) ? cleanStackConfig : defaultPatterns;
|
|
@@ -350,13 +324,12 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
350
324
|
name: this.name,
|
|
351
325
|
code: this.code,
|
|
352
326
|
uiMessage: this.uiMessage,
|
|
353
|
-
cause: this.cause,
|
|
354
327
|
metadata: { ...this.metadata ?? {}, ...additionalMetadata },
|
|
355
|
-
|
|
356
|
-
docsUrl: this.docsUrl,
|
|
357
|
-
source: this.source
|
|
328
|
+
httpStatus: this.httpStatus
|
|
358
329
|
};
|
|
359
330
|
const newError = new _ErrorX(options);
|
|
331
|
+
newError._chain = [newError, ...this._chain.slice(1)];
|
|
332
|
+
newError.original = this.original;
|
|
360
333
|
if (this.stack) {
|
|
361
334
|
newError.stack = this.stack;
|
|
362
335
|
}
|
|
@@ -401,9 +374,7 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
401
374
|
let uiMessage = "";
|
|
402
375
|
let cause;
|
|
403
376
|
let metadata = {};
|
|
404
|
-
let
|
|
405
|
-
let href;
|
|
406
|
-
let source;
|
|
377
|
+
let httpStatus;
|
|
407
378
|
if (error) {
|
|
408
379
|
if (typeof error === "string") {
|
|
409
380
|
message = error;
|
|
@@ -425,26 +396,16 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
425
396
|
if ("code" in error && error.code) code = String(error.code);
|
|
426
397
|
if ("uiMessage" in error && error.uiMessage) uiMessage = String(error.uiMessage);
|
|
427
398
|
else if ("userMessage" in error && error.userMessage) uiMessage = String(error.userMessage);
|
|
428
|
-
if ("type" in error && error.type) {
|
|
429
|
-
type = _ErrorX.validateType(String(error.type));
|
|
430
|
-
}
|
|
431
|
-
if ("docsUrl" in error && error.docsUrl) {
|
|
432
|
-
href = String(error.docsUrl);
|
|
433
|
-
} else if ("href" in error && error.href) {
|
|
434
|
-
href = String(error.href);
|
|
435
|
-
} else if ("documentationUrl" in error && error.documentationUrl) {
|
|
436
|
-
href = String(error.documentationUrl);
|
|
437
|
-
}
|
|
438
|
-
if ("source" in error && error.source) {
|
|
439
|
-
source = String(error.source);
|
|
440
|
-
} else if ("service" in error && error.service) {
|
|
441
|
-
source = String(error.service);
|
|
442
|
-
} else if ("component" in error && error.component) {
|
|
443
|
-
source = String(error.component);
|
|
444
|
-
}
|
|
445
399
|
if ("metadata" in error && typeof error.metadata === "object" && error.metadata !== null) {
|
|
446
400
|
metadata = error.metadata;
|
|
447
401
|
}
|
|
402
|
+
if ("httpStatus" in error && typeof error.httpStatus === "number") {
|
|
403
|
+
httpStatus = error.httpStatus;
|
|
404
|
+
} else if ("status" in error && typeof error.status === "number") {
|
|
405
|
+
httpStatus = error.status;
|
|
406
|
+
} else if ("statusCode" in error && typeof error.statusCode === "number") {
|
|
407
|
+
httpStatus = error.statusCode;
|
|
408
|
+
}
|
|
448
409
|
}
|
|
449
410
|
}
|
|
450
411
|
const options = {
|
|
@@ -455,15 +416,40 @@ var ErrorX = class _ErrorX extends Error {
|
|
|
455
416
|
if (uiMessage) options.uiMessage = uiMessage;
|
|
456
417
|
if (cause) options.cause = cause;
|
|
457
418
|
if (Object.keys(metadata).length > 0) options.metadata = metadata;
|
|
458
|
-
if (
|
|
459
|
-
if (href) options.docsUrl = href;
|
|
460
|
-
if (source) options.source = source;
|
|
419
|
+
if (httpStatus !== void 0) options.httpStatus = httpStatus;
|
|
461
420
|
return options;
|
|
462
421
|
}
|
|
463
|
-
static from(
|
|
464
|
-
if (
|
|
465
|
-
|
|
466
|
-
|
|
422
|
+
static from(payload, overrides) {
|
|
423
|
+
if (payload instanceof _ErrorX) {
|
|
424
|
+
if (overrides && Object.keys(overrides).length > 0) {
|
|
425
|
+
const mergedOptions = {
|
|
426
|
+
message: overrides.message ?? payload.message,
|
|
427
|
+
name: overrides.name ?? payload.name,
|
|
428
|
+
code: overrides.code ?? payload.code,
|
|
429
|
+
uiMessage: overrides.uiMessage ?? payload.uiMessage,
|
|
430
|
+
httpStatus: overrides.httpStatus ?? payload.httpStatus,
|
|
431
|
+
metadata: overrides.metadata ? deepmergeTs.deepmerge(payload.metadata ?? {}, overrides.metadata) : payload.metadata
|
|
432
|
+
};
|
|
433
|
+
const newError = new _ErrorX(mergedOptions);
|
|
434
|
+
newError.original = payload.original;
|
|
435
|
+
newError._chain = [newError];
|
|
436
|
+
return newError;
|
|
437
|
+
}
|
|
438
|
+
return payload;
|
|
439
|
+
}
|
|
440
|
+
const extractedOptions = _ErrorX.convertUnknownToOptions(payload);
|
|
441
|
+
const finalOptions = overrides ? {
|
|
442
|
+
...extractedOptions,
|
|
443
|
+
...overrides,
|
|
444
|
+
metadata: extractedOptions.metadata || overrides.metadata ? deepmergeTs.deepmerge(extractedOptions.metadata ?? {}, overrides.metadata ?? {}) : void 0
|
|
445
|
+
} : extractedOptions;
|
|
446
|
+
const error = new _ErrorX(finalOptions);
|
|
447
|
+
error.original = _ErrorX.toErrorXCause(payload);
|
|
448
|
+
if (payload instanceof Error && payload.stack) {
|
|
449
|
+
error.stack = payload.stack;
|
|
450
|
+
}
|
|
451
|
+
error._chain = [error];
|
|
452
|
+
return error;
|
|
467
453
|
}
|
|
468
454
|
/**
|
|
469
455
|
* Converts the ErrorX instance to a detailed string representation.
|
|
@@ -533,20 +519,28 @@ ${this.stack}`;
|
|
|
533
519
|
metadata: safeMetadata,
|
|
534
520
|
timestamp: this.timestamp
|
|
535
521
|
};
|
|
536
|
-
if (this.
|
|
537
|
-
serialized.
|
|
538
|
-
}
|
|
539
|
-
if (this.docsUrl !== void 0) {
|
|
540
|
-
serialized.docsUrl = this.docsUrl;
|
|
541
|
-
}
|
|
542
|
-
if (this.source !== void 0) {
|
|
543
|
-
serialized.source = this.source;
|
|
522
|
+
if (this.httpStatus !== void 0) {
|
|
523
|
+
serialized.httpStatus = this.httpStatus;
|
|
544
524
|
}
|
|
545
525
|
if (this.stack) {
|
|
546
526
|
serialized.stack = this.stack;
|
|
547
527
|
}
|
|
548
|
-
if (this.
|
|
549
|
-
serialized.
|
|
528
|
+
if (this.original) {
|
|
529
|
+
serialized.original = this.original;
|
|
530
|
+
}
|
|
531
|
+
if (this._chain.length > 1) {
|
|
532
|
+
serialized.chain = this._chain.map((err) => {
|
|
533
|
+
const causeEntry = {
|
|
534
|
+
message: err.message
|
|
535
|
+
};
|
|
536
|
+
if (err.name) {
|
|
537
|
+
causeEntry.name = err.name;
|
|
538
|
+
}
|
|
539
|
+
if (err.stack) {
|
|
540
|
+
causeEntry.stack = err.stack;
|
|
541
|
+
}
|
|
542
|
+
return causeEntry;
|
|
543
|
+
});
|
|
550
544
|
}
|
|
551
545
|
return serialized;
|
|
552
546
|
}
|
|
@@ -578,10 +572,7 @@ ${this.stack}`;
|
|
|
578
572
|
name: serialized.name,
|
|
579
573
|
code: serialized.code,
|
|
580
574
|
uiMessage: serialized.uiMessage,
|
|
581
|
-
|
|
582
|
-
docsUrl: serialized.docsUrl,
|
|
583
|
-
source: serialized.source,
|
|
584
|
-
cause: serialized.cause
|
|
575
|
+
httpStatus: serialized.httpStatus
|
|
585
576
|
};
|
|
586
577
|
if (serialized.metadata !== void 0) {
|
|
587
578
|
options.metadata = serialized.metadata;
|
|
@@ -591,290 +582,629 @@ ${this.stack}`;
|
|
|
591
582
|
error.stack = serialized.stack;
|
|
592
583
|
}
|
|
593
584
|
error.timestamp = serialized.timestamp;
|
|
585
|
+
if (serialized.original) {
|
|
586
|
+
error.original = serialized.original;
|
|
587
|
+
}
|
|
588
|
+
if (serialized.chain && serialized.chain.length > 0) {
|
|
589
|
+
const chainErrors = [error];
|
|
590
|
+
for (let i = 1; i < serialized.chain.length; i++) {
|
|
591
|
+
const causeData = serialized.chain[i];
|
|
592
|
+
if (!causeData) continue;
|
|
593
|
+
const chainErrorOptions = {
|
|
594
|
+
message: causeData.message
|
|
595
|
+
};
|
|
596
|
+
if (causeData.name) {
|
|
597
|
+
chainErrorOptions.name = causeData.name;
|
|
598
|
+
}
|
|
599
|
+
const chainError = new _ErrorX(chainErrorOptions);
|
|
600
|
+
if (causeData.stack) {
|
|
601
|
+
chainError.stack = causeData.stack;
|
|
602
|
+
}
|
|
603
|
+
chainErrors.push(chainError);
|
|
604
|
+
}
|
|
605
|
+
error._chain = chainErrors;
|
|
606
|
+
}
|
|
594
607
|
return error;
|
|
595
608
|
}
|
|
609
|
+
/**
|
|
610
|
+
* Creates a new instance of this error class using optional presets and overrides.
|
|
611
|
+
* This is a factory method that supports preset-based error creation with
|
|
612
|
+
* full TypeScript autocomplete for preset keys.
|
|
613
|
+
*
|
|
614
|
+
* Define static properties on your subclass to customize behavior:
|
|
615
|
+
* - `presets`: Record of preset configurations keyed by identifier
|
|
616
|
+
* - `defaultPreset`: Key of preset to use as fallback
|
|
617
|
+
* - `defaults`: Default values for all errors of this class
|
|
618
|
+
* - `transform`: Function to transform options before instantiation
|
|
619
|
+
*
|
|
620
|
+
* Supported call signatures:
|
|
621
|
+
* - `create()` - uses defaultPreset
|
|
622
|
+
* - `create(presetKey)` - uses specified preset
|
|
623
|
+
* - `create(presetKey, overrides)` - preset with overrides
|
|
624
|
+
* - `create(overrides)` - just overrides, uses defaultPreset
|
|
625
|
+
*
|
|
626
|
+
* @param presetKeyOrOverrides - Preset key (string/number) or overrides object
|
|
627
|
+
* @param overrides - Optional overrides when first arg is preset key
|
|
628
|
+
* @returns New instance of this error class
|
|
629
|
+
*
|
|
630
|
+
* @example
|
|
631
|
+
* ```typescript
|
|
632
|
+
* class DBError extends ErrorX<{ query?: string }> {
|
|
633
|
+
* static presets = {
|
|
634
|
+
* 9333: { message: 'Connection timeout', code: 'TIMEOUT' },
|
|
635
|
+
* CONN_REFUSED: { message: 'Connection refused', code: 'CONN_REFUSED' },
|
|
636
|
+
* GENERIC: { message: 'A database error occurred', code: 'ERROR' },
|
|
637
|
+
* }
|
|
638
|
+
* static defaultPreset = 'GENERIC'
|
|
639
|
+
* static defaults = { httpStatus: 500 }
|
|
640
|
+
* static transform = (opts, ctx) => ({
|
|
641
|
+
* ...opts,
|
|
642
|
+
* code: `DB_${opts.code}`,
|
|
643
|
+
* })
|
|
644
|
+
* }
|
|
645
|
+
*
|
|
646
|
+
* DBError.create() // uses defaultPreset
|
|
647
|
+
* DBError.create(9333) // uses preset 9333
|
|
648
|
+
* DBError.create('CONN_REFUSED') // uses preset CONN_REFUSED
|
|
649
|
+
* DBError.create(9333, { message: 'Custom' }) // preset + overrides
|
|
650
|
+
* DBError.create({ message: 'Custom' }) // just overrides
|
|
651
|
+
* ```
|
|
652
|
+
*/
|
|
653
|
+
static create(presetKeyOrOverrides, overrides) {
|
|
654
|
+
let presetKey;
|
|
655
|
+
let finalOverrides;
|
|
656
|
+
if (typeof presetKeyOrOverrides === "object" && presetKeyOrOverrides !== null) {
|
|
657
|
+
presetKey = void 0;
|
|
658
|
+
finalOverrides = presetKeyOrOverrides;
|
|
659
|
+
} else {
|
|
660
|
+
presetKey = presetKeyOrOverrides;
|
|
661
|
+
finalOverrides = overrides;
|
|
662
|
+
}
|
|
663
|
+
const ctor = this;
|
|
664
|
+
const presets = ctor.presets ?? {};
|
|
665
|
+
const defaultPreset = ctor.defaultPreset;
|
|
666
|
+
const defaults = ctor.defaults ?? {};
|
|
667
|
+
const transform = ctor.transform;
|
|
668
|
+
let resolvedPreset = {};
|
|
669
|
+
if (presetKey !== void 0) {
|
|
670
|
+
if (presetKey in presets) {
|
|
671
|
+
resolvedPreset = presets[presetKey] ?? {};
|
|
672
|
+
} else if (defaultPreset !== void 0 && defaultPreset in presets) {
|
|
673
|
+
resolvedPreset = presets[defaultPreset] ?? {};
|
|
674
|
+
}
|
|
675
|
+
} else if (defaultPreset !== void 0 && defaultPreset in presets) {
|
|
676
|
+
resolvedPreset = presets[defaultPreset] ?? {};
|
|
677
|
+
}
|
|
678
|
+
const mergedOptions = deepmergeTs.deepmerge(
|
|
679
|
+
defaults,
|
|
680
|
+
resolvedPreset,
|
|
681
|
+
finalOverrides ?? {}
|
|
682
|
+
);
|
|
683
|
+
const transformContext = { presetKey };
|
|
684
|
+
const finalOptions = transform ? transform(mergedOptions, transformContext) : mergedOptions;
|
|
685
|
+
return new this(finalOptions);
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
// src/presets/db-error.ts
|
|
690
|
+
var dbPresets = {
|
|
691
|
+
// Connection errors
|
|
692
|
+
CONNECTION_FAILED: {
|
|
693
|
+
code: "CONNECTION_FAILED",
|
|
694
|
+
name: "DBConnectionError",
|
|
695
|
+
message: "Failed to connect to database.",
|
|
696
|
+
uiMessage: "Unable to connect to the database. Please try again later."
|
|
697
|
+
},
|
|
698
|
+
CONNECTION_TIMEOUT: {
|
|
699
|
+
code: "CONNECTION_TIMEOUT",
|
|
700
|
+
name: "DBConnectionTimeoutError",
|
|
701
|
+
message: "Database connection timed out.",
|
|
702
|
+
uiMessage: "The database connection timed out. Please try again."
|
|
703
|
+
},
|
|
704
|
+
CONNECTION_REFUSED: {
|
|
705
|
+
code: "CONNECTION_REFUSED",
|
|
706
|
+
name: "DBConnectionRefusedError",
|
|
707
|
+
message: "Database connection refused.",
|
|
708
|
+
uiMessage: "Unable to connect to the database. Please try again later."
|
|
709
|
+
},
|
|
710
|
+
CONNECTION_LOST: {
|
|
711
|
+
code: "CONNECTION_LOST",
|
|
712
|
+
name: "DBConnectionLostError",
|
|
713
|
+
message: "Database connection lost.",
|
|
714
|
+
uiMessage: "The database connection was lost. Please try again."
|
|
715
|
+
},
|
|
716
|
+
// Query errors
|
|
717
|
+
QUERY_FAILED: {
|
|
718
|
+
code: "QUERY_FAILED",
|
|
719
|
+
name: "DBQueryError",
|
|
720
|
+
message: "Database query failed.",
|
|
721
|
+
uiMessage: "The database operation failed. Please try again."
|
|
722
|
+
},
|
|
723
|
+
QUERY_TIMEOUT: {
|
|
724
|
+
code: "QUERY_TIMEOUT",
|
|
725
|
+
name: "DBQueryTimeoutError",
|
|
726
|
+
message: "Database query timed out.",
|
|
727
|
+
uiMessage: "The database operation took too long. Please try again."
|
|
728
|
+
},
|
|
729
|
+
SYNTAX_ERROR: {
|
|
730
|
+
code: "SYNTAX_ERROR",
|
|
731
|
+
name: "DBSyntaxError",
|
|
732
|
+
message: "Invalid query syntax.",
|
|
733
|
+
uiMessage: "An internal error occurred. Please contact support."
|
|
734
|
+
},
|
|
735
|
+
// Constraint errors
|
|
736
|
+
UNIQUE_VIOLATION: {
|
|
737
|
+
code: "UNIQUE_VIOLATION",
|
|
738
|
+
name: "DBUniqueViolationError",
|
|
739
|
+
message: "Unique constraint violation.",
|
|
740
|
+
uiMessage: "This record already exists.",
|
|
741
|
+
httpStatus: 409
|
|
742
|
+
},
|
|
743
|
+
FOREIGN_KEY_VIOLATION: {
|
|
744
|
+
code: "FOREIGN_KEY_VIOLATION",
|
|
745
|
+
name: "DBForeignKeyError",
|
|
746
|
+
message: "Foreign key constraint violation.",
|
|
747
|
+
uiMessage: "This operation references a record that does not exist.",
|
|
748
|
+
httpStatus: 400
|
|
749
|
+
},
|
|
750
|
+
NOT_NULL_VIOLATION: {
|
|
751
|
+
code: "NOT_NULL_VIOLATION",
|
|
752
|
+
name: "DBNotNullError",
|
|
753
|
+
message: "Not null constraint violation.",
|
|
754
|
+
uiMessage: "A required field is missing.",
|
|
755
|
+
httpStatus: 400
|
|
756
|
+
},
|
|
757
|
+
CHECK_VIOLATION: {
|
|
758
|
+
code: "CHECK_VIOLATION",
|
|
759
|
+
name: "DBCheckViolationError",
|
|
760
|
+
message: "Check constraint violation.",
|
|
761
|
+
uiMessage: "The provided data is invalid.",
|
|
762
|
+
httpStatus: 400
|
|
763
|
+
},
|
|
764
|
+
// Transaction errors
|
|
765
|
+
TRANSACTION_FAILED: {
|
|
766
|
+
code: "TRANSACTION_FAILED",
|
|
767
|
+
name: "DBTransactionError",
|
|
768
|
+
message: "Database transaction failed.",
|
|
769
|
+
uiMessage: "The operation failed. Please try again."
|
|
770
|
+
},
|
|
771
|
+
DEADLOCK: {
|
|
772
|
+
code: "DEADLOCK",
|
|
773
|
+
name: "DBDeadlockError",
|
|
774
|
+
message: "Database deadlock detected.",
|
|
775
|
+
uiMessage: "The operation encountered a conflict. Please try again.",
|
|
776
|
+
httpStatus: 409
|
|
777
|
+
},
|
|
778
|
+
// Record errors
|
|
779
|
+
NOT_FOUND: {
|
|
780
|
+
code: "NOT_FOUND",
|
|
781
|
+
name: "DBNotFoundError",
|
|
782
|
+
message: "Record not found.",
|
|
783
|
+
uiMessage: "The requested record was not found.",
|
|
784
|
+
httpStatus: 404
|
|
785
|
+
},
|
|
786
|
+
// Generic
|
|
787
|
+
UNKNOWN: {
|
|
788
|
+
code: "UNKNOWN",
|
|
789
|
+
name: "DBErrorX",
|
|
790
|
+
message: "An unknown database error occurred.",
|
|
791
|
+
uiMessage: "A database error occurred. Please try again later."
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
var DBErrorX = class _DBErrorX extends ErrorX {
|
|
795
|
+
/**
|
|
796
|
+
* Database error presets for common scenarios.
|
|
797
|
+
*/
|
|
798
|
+
static presets = dbPresets;
|
|
799
|
+
/** Default to UNKNOWN when no preset specified */
|
|
800
|
+
static defaultPreset = "UNKNOWN";
|
|
801
|
+
/** Default httpStatus for database errors (500 = server error) */
|
|
802
|
+
static defaults = { httpStatus: 500 };
|
|
803
|
+
/**
|
|
804
|
+
* Transform that prefixes all codes with `DB_`.
|
|
805
|
+
*/
|
|
806
|
+
static transform = (opts, _ctx) => {
|
|
807
|
+
const code = String(opts.code ?? "UNKNOWN");
|
|
808
|
+
return {
|
|
809
|
+
...opts,
|
|
810
|
+
code: code.startsWith("DB_") ? code : `DB_${code}`
|
|
811
|
+
};
|
|
812
|
+
};
|
|
813
|
+
static create(presetKeyOrOverrides, overrides) {
|
|
814
|
+
return ErrorX.create.call(
|
|
815
|
+
_DBErrorX,
|
|
816
|
+
presetKeyOrOverrides,
|
|
817
|
+
overrides
|
|
818
|
+
);
|
|
819
|
+
}
|
|
596
820
|
};
|
|
597
821
|
|
|
598
|
-
// src/presets.ts
|
|
599
|
-
var
|
|
822
|
+
// src/presets/http-error.ts
|
|
823
|
+
var httpPresets = {
|
|
600
824
|
// 4xx Client Errors
|
|
601
825
|
400: {
|
|
602
826
|
code: "BAD_REQUEST",
|
|
603
|
-
name: "
|
|
827
|
+
name: "BadRequestError",
|
|
604
828
|
message: "Bad request.",
|
|
605
|
-
uiMessage: "The request could not be processed. Please check your input and try again."
|
|
606
|
-
metadata: { status: 400 }
|
|
829
|
+
uiMessage: "The request could not be processed. Please check your input and try again."
|
|
607
830
|
},
|
|
608
831
|
401: {
|
|
609
832
|
code: "UNAUTHORIZED",
|
|
610
|
-
name: "
|
|
833
|
+
name: "UnauthorizedError",
|
|
611
834
|
message: "Unauthorized.",
|
|
612
|
-
uiMessage: "Authentication required. Please log in to continue."
|
|
613
|
-
metadata: { status: 401 }
|
|
835
|
+
uiMessage: "Authentication required. Please log in to continue."
|
|
614
836
|
},
|
|
615
837
|
402: {
|
|
616
838
|
code: "PAYMENT_REQUIRED",
|
|
617
|
-
name: "
|
|
839
|
+
name: "PaymentRequiredError",
|
|
618
840
|
message: "Payment required.",
|
|
619
|
-
uiMessage: "Payment is required to access this resource."
|
|
620
|
-
metadata: { status: 402 }
|
|
841
|
+
uiMessage: "Payment is required to access this resource."
|
|
621
842
|
},
|
|
622
843
|
403: {
|
|
623
844
|
code: "FORBIDDEN",
|
|
624
|
-
name: "
|
|
845
|
+
name: "ForbiddenError",
|
|
625
846
|
message: "Forbidden.",
|
|
626
|
-
uiMessage: "You do not have permission to access this resource."
|
|
627
|
-
metadata: { status: 403 }
|
|
847
|
+
uiMessage: "You do not have permission to access this resource."
|
|
628
848
|
},
|
|
629
849
|
404: {
|
|
630
850
|
code: "NOT_FOUND",
|
|
631
|
-
name: "
|
|
851
|
+
name: "NotFoundError",
|
|
632
852
|
message: "Not found.",
|
|
633
|
-
uiMessage: "The requested resource could not be found."
|
|
634
|
-
metadata: { status: 404 }
|
|
853
|
+
uiMessage: "The requested resource could not be found."
|
|
635
854
|
},
|
|
636
855
|
405: {
|
|
637
856
|
code: "METHOD_NOT_ALLOWED",
|
|
638
|
-
name: "
|
|
857
|
+
name: "MethodNotAllowedError",
|
|
639
858
|
message: "Method not allowed.",
|
|
640
|
-
uiMessage: "This action is not allowed for the requested resource."
|
|
641
|
-
metadata: { status: 405 }
|
|
859
|
+
uiMessage: "This action is not allowed for the requested resource."
|
|
642
860
|
},
|
|
643
861
|
406: {
|
|
644
862
|
code: "NOT_ACCEPTABLE",
|
|
645
|
-
name: "
|
|
863
|
+
name: "NotAcceptableError",
|
|
646
864
|
message: "Not acceptable.",
|
|
647
|
-
uiMessage: "The requested format is not supported."
|
|
648
|
-
metadata: { status: 406 }
|
|
865
|
+
uiMessage: "The requested format is not supported."
|
|
649
866
|
},
|
|
650
867
|
407: {
|
|
651
868
|
code: "PROXY_AUTHENTICATION_REQUIRED",
|
|
652
|
-
name: "
|
|
869
|
+
name: "ProxyAuthenticationRequiredError",
|
|
653
870
|
message: "Proxy authentication required.",
|
|
654
|
-
uiMessage: "Proxy authentication is required to access this resource."
|
|
655
|
-
metadata: { status: 407 }
|
|
871
|
+
uiMessage: "Proxy authentication is required to access this resource."
|
|
656
872
|
},
|
|
657
873
|
408: {
|
|
658
874
|
code: "REQUEST_TIMEOUT",
|
|
659
|
-
name: "
|
|
875
|
+
name: "RequestTimeoutError",
|
|
660
876
|
message: "Request timeout.",
|
|
661
|
-
uiMessage: "The request took too long to complete. Please try again."
|
|
662
|
-
metadata: { status: 408 }
|
|
877
|
+
uiMessage: "The request took too long to complete. Please try again."
|
|
663
878
|
},
|
|
664
879
|
409: {
|
|
665
880
|
code: "CONFLICT",
|
|
666
|
-
name: "
|
|
881
|
+
name: "ConflictError",
|
|
667
882
|
message: "Conflict.",
|
|
668
|
-
uiMessage: "The request conflicts with the current state. Please refresh and try again."
|
|
669
|
-
metadata: { status: 409 }
|
|
883
|
+
uiMessage: "The request conflicts with the current state. Please refresh and try again."
|
|
670
884
|
},
|
|
671
885
|
410: {
|
|
672
886
|
code: "GONE",
|
|
673
|
-
name: "
|
|
887
|
+
name: "GoneError",
|
|
674
888
|
message: "Gone.",
|
|
675
|
-
uiMessage: "This resource is no longer available."
|
|
676
|
-
metadata: { status: 410 }
|
|
889
|
+
uiMessage: "This resource is no longer available."
|
|
677
890
|
},
|
|
678
891
|
411: {
|
|
679
892
|
code: "LENGTH_REQUIRED",
|
|
680
|
-
name: "
|
|
893
|
+
name: "LengthRequiredError",
|
|
681
894
|
message: "Length required.",
|
|
682
|
-
uiMessage: "The request is missing required length information."
|
|
683
|
-
metadata: { status: 411 }
|
|
895
|
+
uiMessage: "The request is missing required length information."
|
|
684
896
|
},
|
|
685
897
|
412: {
|
|
686
898
|
code: "PRECONDITION_FAILED",
|
|
687
|
-
name: "
|
|
899
|
+
name: "PreconditionFailedError",
|
|
688
900
|
message: "Precondition failed.",
|
|
689
|
-
uiMessage: "A required condition was not met. Please try again."
|
|
690
|
-
metadata: { status: 412 }
|
|
901
|
+
uiMessage: "A required condition was not met. Please try again."
|
|
691
902
|
},
|
|
692
903
|
413: {
|
|
693
904
|
code: "PAYLOAD_TOO_LARGE",
|
|
694
|
-
name: "
|
|
905
|
+
name: "PayloadTooLargeError",
|
|
695
906
|
message: "Payload too large.",
|
|
696
|
-
uiMessage: "The request is too large. Please reduce the size and try again."
|
|
697
|
-
metadata: { status: 413 }
|
|
907
|
+
uiMessage: "The request is too large. Please reduce the size and try again."
|
|
698
908
|
},
|
|
699
909
|
414: {
|
|
700
910
|
code: "URI_TOO_LONG",
|
|
701
|
-
name: "
|
|
911
|
+
name: "UriTooLongError",
|
|
702
912
|
message: "URI too long.",
|
|
703
|
-
uiMessage: "The request URL is too long."
|
|
704
|
-
metadata: { status: 414 }
|
|
913
|
+
uiMessage: "The request URL is too long."
|
|
705
914
|
},
|
|
706
915
|
415: {
|
|
707
916
|
code: "UNSUPPORTED_MEDIA_TYPE",
|
|
708
|
-
name: "
|
|
917
|
+
name: "UnsupportedMediaTypeError",
|
|
709
918
|
message: "Unsupported media type.",
|
|
710
|
-
uiMessage: "The file type is not supported."
|
|
711
|
-
metadata: { status: 415 }
|
|
919
|
+
uiMessage: "The file type is not supported."
|
|
712
920
|
},
|
|
713
921
|
416: {
|
|
714
922
|
code: "RANGE_NOT_SATISFIABLE",
|
|
715
|
-
name: "
|
|
923
|
+
name: "RangeNotSatisfiableError",
|
|
716
924
|
message: "Range not satisfiable.",
|
|
717
|
-
uiMessage: "The requested range cannot be satisfied."
|
|
718
|
-
metadata: { status: 416 }
|
|
925
|
+
uiMessage: "The requested range cannot be satisfied."
|
|
719
926
|
},
|
|
720
927
|
417: {
|
|
721
928
|
code: "EXPECTATION_FAILED",
|
|
722
|
-
name: "
|
|
929
|
+
name: "ExpectationFailedError",
|
|
723
930
|
message: "Expectation failed.",
|
|
724
|
-
uiMessage: "The server cannot meet the requirements of the request."
|
|
725
|
-
metadata: { status: 417 }
|
|
931
|
+
uiMessage: "The server cannot meet the requirements of the request."
|
|
726
932
|
},
|
|
727
933
|
418: {
|
|
728
934
|
code: "IM_A_TEAPOT",
|
|
729
|
-
name: "
|
|
935
|
+
name: "ImATeapotError",
|
|
730
936
|
message: "I'm a teapot.",
|
|
731
|
-
uiMessage: "I'm a teapot and cannot brew coffee."
|
|
732
|
-
metadata: { status: 418 }
|
|
937
|
+
uiMessage: "I'm a teapot and cannot brew coffee."
|
|
733
938
|
},
|
|
734
939
|
422: {
|
|
735
940
|
code: "UNPROCESSABLE_ENTITY",
|
|
736
|
-
name: "
|
|
941
|
+
name: "UnprocessableEntityError",
|
|
737
942
|
message: "Unprocessable entity.",
|
|
738
|
-
uiMessage: "The request contains invalid data. Please check your input."
|
|
739
|
-
metadata: { status: 422 }
|
|
943
|
+
uiMessage: "The request contains invalid data. Please check your input."
|
|
740
944
|
},
|
|
741
945
|
423: {
|
|
742
946
|
code: "LOCKED",
|
|
743
|
-
name: "
|
|
947
|
+
name: "LockedError",
|
|
744
948
|
message: "Locked.",
|
|
745
|
-
uiMessage: "This resource is locked and cannot be modified."
|
|
746
|
-
metadata: { status: 423 }
|
|
949
|
+
uiMessage: "This resource is locked and cannot be modified."
|
|
747
950
|
},
|
|
748
951
|
424: {
|
|
749
952
|
code: "FAILED_DEPENDENCY",
|
|
750
|
-
name: "
|
|
953
|
+
name: "FailedDependencyError",
|
|
751
954
|
message: "Failed dependency.",
|
|
752
|
-
uiMessage: "The request failed due to a dependency error."
|
|
753
|
-
metadata: { status: 424 }
|
|
955
|
+
uiMessage: "The request failed due to a dependency error."
|
|
754
956
|
},
|
|
755
957
|
425: {
|
|
756
958
|
code: "TOO_EARLY",
|
|
757
|
-
name: "
|
|
959
|
+
name: "TooEarlyError",
|
|
758
960
|
message: "Too early.",
|
|
759
|
-
uiMessage: "The request was sent too early. Please try again later."
|
|
760
|
-
metadata: { status: 425 }
|
|
961
|
+
uiMessage: "The request was sent too early. Please try again later."
|
|
761
962
|
},
|
|
762
963
|
426: {
|
|
763
964
|
code: "UPGRADE_REQUIRED",
|
|
764
|
-
name: "
|
|
965
|
+
name: "UpgradeRequiredError",
|
|
765
966
|
message: "Upgrade required.",
|
|
766
|
-
uiMessage: "Please upgrade to continue using this service."
|
|
767
|
-
metadata: { status: 426 }
|
|
967
|
+
uiMessage: "Please upgrade to continue using this service."
|
|
768
968
|
},
|
|
769
969
|
428: {
|
|
770
970
|
code: "PRECONDITION_REQUIRED",
|
|
771
|
-
name: "
|
|
971
|
+
name: "PreconditionRequiredError",
|
|
772
972
|
message: "Precondition required.",
|
|
773
|
-
uiMessage: "Required conditions are missing from the request."
|
|
774
|
-
metadata: { status: 428 }
|
|
973
|
+
uiMessage: "Required conditions are missing from the request."
|
|
775
974
|
},
|
|
776
975
|
429: {
|
|
777
976
|
code: "TOO_MANY_REQUESTS",
|
|
778
|
-
name: "
|
|
977
|
+
name: "TooManyRequestsError",
|
|
779
978
|
message: "Too many requests.",
|
|
780
|
-
uiMessage: "You have made too many requests. Please wait and try again."
|
|
781
|
-
metadata: { status: 429 }
|
|
979
|
+
uiMessage: "You have made too many requests. Please wait and try again."
|
|
782
980
|
},
|
|
783
981
|
431: {
|
|
784
982
|
code: "REQUEST_HEADER_FIELDS_TOO_LARGE",
|
|
785
|
-
name: "
|
|
983
|
+
name: "RequestHeaderFieldsTooLargeError",
|
|
786
984
|
message: "Request header fields too large.",
|
|
787
|
-
uiMessage: "The request headers are too large."
|
|
788
|
-
metadata: { status: 431 }
|
|
985
|
+
uiMessage: "The request headers are too large."
|
|
789
986
|
},
|
|
790
987
|
451: {
|
|
791
988
|
code: "UNAVAILABLE_FOR_LEGAL_REASONS",
|
|
792
|
-
name: "
|
|
989
|
+
name: "UnavailableForLegalReasonsError",
|
|
793
990
|
message: "Unavailable for legal reasons.",
|
|
794
|
-
uiMessage: "This content is unavailable for legal reasons."
|
|
795
|
-
metadata: { status: 451 }
|
|
991
|
+
uiMessage: "This content is unavailable for legal reasons."
|
|
796
992
|
},
|
|
797
993
|
// 5xx Server Errors
|
|
798
994
|
500: {
|
|
799
995
|
code: "INTERNAL_SERVER_ERROR",
|
|
800
|
-
name: "
|
|
996
|
+
name: "InternalServerError",
|
|
801
997
|
message: "Internal server error.",
|
|
802
|
-
uiMessage: "An unexpected error occurred. Please try again later."
|
|
803
|
-
metadata: { status: 500 }
|
|
998
|
+
uiMessage: "An unexpected error occurred. Please try again later."
|
|
804
999
|
},
|
|
805
1000
|
501: {
|
|
806
1001
|
code: "NOT_IMPLEMENTED",
|
|
807
|
-
name: "
|
|
1002
|
+
name: "NotImplementedError",
|
|
808
1003
|
message: "Not implemented.",
|
|
809
|
-
uiMessage: "This feature is not yet available."
|
|
810
|
-
metadata: { status: 501 }
|
|
1004
|
+
uiMessage: "This feature is not yet available."
|
|
811
1005
|
},
|
|
812
1006
|
502: {
|
|
813
1007
|
code: "BAD_GATEWAY",
|
|
814
|
-
name: "
|
|
1008
|
+
name: "BadGatewayError",
|
|
815
1009
|
message: "Bad gateway.",
|
|
816
|
-
uiMessage: "Unable to connect to the server. Please try again later."
|
|
817
|
-
metadata: { status: 502 }
|
|
1010
|
+
uiMessage: "Unable to connect to the server. Please try again later."
|
|
818
1011
|
},
|
|
819
1012
|
503: {
|
|
820
1013
|
code: "SERVICE_UNAVAILABLE",
|
|
821
|
-
name: "
|
|
1014
|
+
name: "ServiceUnavailableError",
|
|
822
1015
|
message: "Service unavailable.",
|
|
823
|
-
uiMessage: "The service is temporarily unavailable. Please try again later."
|
|
824
|
-
metadata: { status: 503 }
|
|
1016
|
+
uiMessage: "The service is temporarily unavailable. Please try again later."
|
|
825
1017
|
},
|
|
826
1018
|
504: {
|
|
827
1019
|
code: "GATEWAY_TIMEOUT",
|
|
828
|
-
name: "
|
|
1020
|
+
name: "GatewayTimeoutError",
|
|
829
1021
|
message: "Gateway timeout.",
|
|
830
|
-
uiMessage: "The server took too long to respond. Please try again."
|
|
831
|
-
metadata: { status: 504 }
|
|
1022
|
+
uiMessage: "The server took too long to respond. Please try again."
|
|
832
1023
|
},
|
|
833
1024
|
505: {
|
|
834
1025
|
code: "HTTP_VERSION_NOT_SUPPORTED",
|
|
835
|
-
name: "
|
|
1026
|
+
name: "HttpVersionNotSupportedError",
|
|
836
1027
|
message: "HTTP version not supported.",
|
|
837
|
-
uiMessage: "Your browser version is not supported."
|
|
838
|
-
metadata: { status: 505 }
|
|
1028
|
+
uiMessage: "Your browser version is not supported."
|
|
839
1029
|
},
|
|
840
1030
|
506: {
|
|
841
1031
|
code: "VARIANT_ALSO_NEGOTIATES",
|
|
842
|
-
name: "
|
|
1032
|
+
name: "VariantAlsoNegotiatesError",
|
|
843
1033
|
message: "Variant also negotiates.",
|
|
844
|
-
uiMessage: "The server has an internal configuration error."
|
|
845
|
-
metadata: { status: 506 }
|
|
1034
|
+
uiMessage: "The server has an internal configuration error."
|
|
846
1035
|
},
|
|
847
1036
|
507: {
|
|
848
1037
|
code: "INSUFFICIENT_STORAGE",
|
|
849
|
-
name: "
|
|
1038
|
+
name: "InsufficientStorageError",
|
|
850
1039
|
message: "Insufficient storage.",
|
|
851
|
-
uiMessage: "The server has insufficient storage to complete the request."
|
|
852
|
-
metadata: { status: 507 }
|
|
1040
|
+
uiMessage: "The server has insufficient storage to complete the request."
|
|
853
1041
|
},
|
|
854
1042
|
508: {
|
|
855
1043
|
code: "LOOP_DETECTED",
|
|
856
|
-
name: "
|
|
1044
|
+
name: "LoopDetectedError",
|
|
857
1045
|
message: "Loop detected.",
|
|
858
|
-
uiMessage: "The server detected an infinite loop."
|
|
859
|
-
metadata: { status: 508 }
|
|
1046
|
+
uiMessage: "The server detected an infinite loop."
|
|
860
1047
|
},
|
|
861
1048
|
510: {
|
|
862
1049
|
code: "NOT_EXTENDED",
|
|
863
|
-
name: "
|
|
1050
|
+
name: "NotExtendedError",
|
|
864
1051
|
message: "Not extended.",
|
|
865
|
-
uiMessage: "Additional extensions are required."
|
|
866
|
-
metadata: { status: 510 }
|
|
1052
|
+
uiMessage: "Additional extensions are required."
|
|
867
1053
|
},
|
|
868
1054
|
511: {
|
|
869
1055
|
code: "NETWORK_AUTHENTICATION_REQUIRED",
|
|
870
|
-
name: "
|
|
1056
|
+
name: "NetworkAuthenticationRequiredError",
|
|
871
1057
|
message: "Network authentication required.",
|
|
872
|
-
uiMessage: "Network authentication is required to access this resource."
|
|
873
|
-
|
|
1058
|
+
uiMessage: "Network authentication is required to access this resource."
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
var HTTPErrorX = class _HTTPErrorX extends ErrorX {
|
|
1062
|
+
/**
|
|
1063
|
+
* HTTP status code presets for all standard codes.
|
|
1064
|
+
* Keys are numeric status codes (400, 401, 404, 500, etc.)
|
|
1065
|
+
*/
|
|
1066
|
+
static presets = httpPresets;
|
|
1067
|
+
/** Default to 500 Internal Server Error when no preset specified */
|
|
1068
|
+
static defaultPreset = 500;
|
|
1069
|
+
/** Default httpStatus for all HTTPErrorXs */
|
|
1070
|
+
static defaults = { httpStatus: 500 };
|
|
1071
|
+
/**
|
|
1072
|
+
* Transform that automatically sets httpStatus from the preset key.
|
|
1073
|
+
* Only sets httpStatus from presetKey if it matches a known preset.
|
|
1074
|
+
*/
|
|
1075
|
+
static transform = (opts, { presetKey }) => ({
|
|
1076
|
+
...opts,
|
|
1077
|
+
httpStatus: typeof presetKey === "number" && presetKey in httpPresets ? presetKey : opts.httpStatus
|
|
1078
|
+
});
|
|
1079
|
+
static create(statusCodeOrOverrides, overrides) {
|
|
1080
|
+
return ErrorX.create.call(
|
|
1081
|
+
_HTTPErrorX,
|
|
1082
|
+
statusCodeOrOverrides,
|
|
1083
|
+
overrides
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
// src/presets/validation-error.ts
|
|
1089
|
+
var ValidationErrorX = class _ValidationErrorX extends ErrorX {
|
|
1090
|
+
/** Default httpStatus for validation errors (400 = bad request) */
|
|
1091
|
+
static defaults = {
|
|
1092
|
+
httpStatus: 400,
|
|
1093
|
+
name: "ValidationErrorX",
|
|
1094
|
+
code: "VALIDATION_ERROR",
|
|
1095
|
+
message: "Validation failed.",
|
|
1096
|
+
uiMessage: "The provided input is invalid. Please check your data."
|
|
1097
|
+
};
|
|
1098
|
+
/**
|
|
1099
|
+
* Transform that maps Zod issue codes to VALIDATION_ prefixed codes.
|
|
1100
|
+
* Converts Zod's snake_case codes to SCREAMING_SNAKE_CASE.
|
|
1101
|
+
*/
|
|
1102
|
+
static transform = (opts, _ctx) => {
|
|
1103
|
+
const code = String(opts.code ?? "ERROR").toUpperCase();
|
|
1104
|
+
return {
|
|
1105
|
+
...opts,
|
|
1106
|
+
code: code.startsWith("VALIDATION_") ? code : `VALIDATION_${code}`
|
|
1107
|
+
};
|
|
1108
|
+
};
|
|
1109
|
+
/**
|
|
1110
|
+
* Creates a ValidationErrorX from a Zod error.
|
|
1111
|
+
*
|
|
1112
|
+
* Maps Zod's error structure to ErrorX:
|
|
1113
|
+
* - Uses first issue's message as the error message
|
|
1114
|
+
* - Converts Zod issue code to ErrorX code (e.g., 'invalid_type' → 'VALIDATION_INVALID_TYPE')
|
|
1115
|
+
* - Captures all issues in metadata for multi-error handling
|
|
1116
|
+
*
|
|
1117
|
+
* @param zodError - The Zod error object (or any object with `issues` array)
|
|
1118
|
+
* @param overrides - Optional overrides for any ErrorX options
|
|
1119
|
+
* @returns ValidationErrorX instance
|
|
1120
|
+
*
|
|
1121
|
+
* @example
|
|
1122
|
+
* ```typescript
|
|
1123
|
+
* // Basic usage
|
|
1124
|
+
* try {
|
|
1125
|
+
* schema.parse(data)
|
|
1126
|
+
* } catch (err) {
|
|
1127
|
+
* if (err instanceof ZodError) {
|
|
1128
|
+
* throw ValidationErrorX.fromZodError(err)
|
|
1129
|
+
* }
|
|
1130
|
+
* }
|
|
1131
|
+
*
|
|
1132
|
+
* // With custom uiMessage
|
|
1133
|
+
* ValidationErrorX.fromZodError(zodError, {
|
|
1134
|
+
* uiMessage: 'Please fix the form errors',
|
|
1135
|
+
* })
|
|
1136
|
+
*
|
|
1137
|
+
* // Access all issues
|
|
1138
|
+
* const error = ValidationErrorX.fromZodError(zodError)
|
|
1139
|
+
* error.metadata?.issues?.forEach(issue => {
|
|
1140
|
+
* console.log(`${issue.path.join('.')}: ${issue.message}`)
|
|
1141
|
+
* })
|
|
1142
|
+
* ```
|
|
1143
|
+
*/
|
|
1144
|
+
static fromZodError(zodError, overrides) {
|
|
1145
|
+
const firstIssue = zodError.issues[0];
|
|
1146
|
+
const fieldPath = firstIssue?.path.join(".");
|
|
1147
|
+
const zodCode = firstIssue?.code ?? "unknown";
|
|
1148
|
+
const errorCode = zodCode.toUpperCase().replace(/-/g, "_");
|
|
1149
|
+
const metadata = {
|
|
1150
|
+
zodCode,
|
|
1151
|
+
issueCount: zodError.issues.length,
|
|
1152
|
+
issues: zodError.issues
|
|
1153
|
+
};
|
|
1154
|
+
if (fieldPath) metadata.field = fieldPath;
|
|
1155
|
+
if (firstIssue?.path) metadata.path = firstIssue.path;
|
|
1156
|
+
if (firstIssue?.expected) metadata.expected = firstIssue.expected;
|
|
1157
|
+
if (firstIssue?.received) metadata.received = firstIssue.received;
|
|
1158
|
+
const mergedOpts = {
|
|
1159
|
+
..._ValidationErrorX.defaults,
|
|
1160
|
+
code: errorCode,
|
|
1161
|
+
message: firstIssue?.message ?? "Validation failed",
|
|
1162
|
+
metadata,
|
|
1163
|
+
...overrides
|
|
1164
|
+
};
|
|
1165
|
+
const transformedOpts = _ValidationErrorX.transform(mergedOpts, { presetKey: void 0 });
|
|
1166
|
+
return new _ValidationErrorX(transformedOpts);
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Creates a ValidationErrorX for a specific field.
|
|
1170
|
+
* Convenience method for manual validation errors.
|
|
1171
|
+
*
|
|
1172
|
+
* @param field - The field name that failed validation
|
|
1173
|
+
* @param message - The error message
|
|
1174
|
+
* @param options - Additional options
|
|
1175
|
+
* @returns ValidationErrorX instance
|
|
1176
|
+
*
|
|
1177
|
+
* @example
|
|
1178
|
+
* ```typescript
|
|
1179
|
+
* // Simple field error
|
|
1180
|
+
* throw ValidationErrorX.forField('email', 'Invalid email format')
|
|
1181
|
+
*
|
|
1182
|
+
* // With code
|
|
1183
|
+
* throw ValidationErrorX.forField('age', 'Must be 18 or older', {
|
|
1184
|
+
* code: 'TOO_YOUNG',
|
|
1185
|
+
* })
|
|
1186
|
+
* ```
|
|
1187
|
+
*/
|
|
1188
|
+
static forField(field, message, options) {
|
|
1189
|
+
const mergedOpts = {
|
|
1190
|
+
..._ValidationErrorX.defaults,
|
|
1191
|
+
message,
|
|
1192
|
+
code: options?.code ?? "INVALID_FIELD",
|
|
1193
|
+
metadata: {
|
|
1194
|
+
field,
|
|
1195
|
+
path: field.split("."),
|
|
1196
|
+
...options?.metadata
|
|
1197
|
+
},
|
|
1198
|
+
...options
|
|
1199
|
+
};
|
|
1200
|
+
const transformedOpts = _ValidationErrorX.transform(mergedOpts, { presetKey: void 0 });
|
|
1201
|
+
return new _ValidationErrorX(transformedOpts);
|
|
874
1202
|
}
|
|
875
1203
|
};
|
|
876
1204
|
|
|
1205
|
+
exports.DBErrorX = DBErrorX;
|
|
877
1206
|
exports.ErrorX = ErrorX;
|
|
878
|
-
exports.
|
|
1207
|
+
exports.HTTPErrorX = HTTPErrorX;
|
|
1208
|
+
exports.ValidationErrorX = ValidationErrorX;
|
|
879
1209
|
//# sourceMappingURL=index.cjs.map
|
|
880
1210
|
//# sourceMappingURL=index.cjs.map
|