@dxos/errors 0.8.4-main.e098934 → 0.8.4-main.e8ec1fe
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/dist/lib/browser/index.mjs +29 -85
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +29 -85
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/base.d.ts +9 -12
- package/dist/types/src/base.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +84 -108
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/base.ts +20 -17
package/package.json
CHANGED
package/src/base.ts
CHANGED
|
@@ -28,15 +28,25 @@ export class BaseError<Code extends string = string> extends Error {
|
|
|
28
28
|
* @param message - Default error message.
|
|
29
29
|
*/
|
|
30
30
|
static extend<Code extends string = string>(code: Code, message?: string) {
|
|
31
|
-
return class extends BaseError<Code> {
|
|
31
|
+
return class ExtendedError extends BaseError<Code> {
|
|
32
32
|
static code = code;
|
|
33
33
|
|
|
34
34
|
static is(error: unknown): error is BaseError {
|
|
35
35
|
return typeof error === 'object' && error !== null && 'code' in error && error.code === code;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
static wrap(
|
|
39
|
-
|
|
38
|
+
static wrap(
|
|
39
|
+
options?: Omit<BaseErrorOptions, 'cause'> & { ifTypeDiffers?: boolean },
|
|
40
|
+
): (error: unknown) => ExtendedError {
|
|
41
|
+
const wrapFn = (error: unknown) => {
|
|
42
|
+
if (options?.ifTypeDiffers === true && this.is(error)) {
|
|
43
|
+
return error as ExtendedError;
|
|
44
|
+
}
|
|
45
|
+
const newError: ExtendedError = new this({ message, ...options, cause: error });
|
|
46
|
+
Error.captureStackTrace(newError, wrapFn); // Position stack-trace to start from the caller of `wrap`.
|
|
47
|
+
return newError;
|
|
48
|
+
};
|
|
49
|
+
return wrapFn;
|
|
40
50
|
}
|
|
41
51
|
|
|
42
52
|
constructor(options?: BaseErrorOptions) {
|
|
@@ -45,19 +55,20 @@ export class BaseError<Code extends string = string> extends Error {
|
|
|
45
55
|
};
|
|
46
56
|
}
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
// NOTE: Errors go through odd transformations and the private fields seem to break.
|
|
59
|
+
code: Code;
|
|
60
|
+
context: Record<string, unknown>;
|
|
50
61
|
|
|
51
62
|
constructor(code: Code, options?: BaseErrorOptions) {
|
|
52
63
|
super(options?.message, { cause: options?.cause });
|
|
53
64
|
|
|
54
|
-
this
|
|
55
|
-
this
|
|
65
|
+
this.code = code;
|
|
66
|
+
this.context = options?.context ?? {};
|
|
56
67
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
57
68
|
}
|
|
58
69
|
|
|
59
70
|
override get name() {
|
|
60
|
-
return this
|
|
71
|
+
return this.code;
|
|
61
72
|
}
|
|
62
73
|
|
|
63
74
|
/** Fallback message. */
|
|
@@ -65,16 +76,8 @@ export class BaseError<Code extends string = string> extends Error {
|
|
|
65
76
|
return this.constructor.name;
|
|
66
77
|
}
|
|
67
78
|
|
|
68
|
-
get code(): Code {
|
|
69
|
-
return this.#code;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
79
|
// For effect error matching.
|
|
73
80
|
get _tag(): Code {
|
|
74
|
-
return this
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get context() {
|
|
78
|
-
return this.#context;
|
|
81
|
+
return this.code;
|
|
79
82
|
}
|
|
80
83
|
}
|