@dxos/errors 0.8.4-main.fd6878d → 0.8.4-main.fffef41
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 +46 -36
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +46 -36
- 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 +28 -28
- package/dist/types/src/base.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +174 -198
- 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 +37 -30
- package/src/errors.test.ts +23 -4
- package/src/errors.ts +6 -6
package/package.json
CHANGED
package/src/base.ts
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Options for creating a BaseError.
|
|
7
|
+
*/
|
|
8
|
+
export type BaseErrorOptions = ErrorOptions & {
|
|
6
9
|
/**
|
|
7
|
-
*
|
|
8
|
-
* An instance of Error.
|
|
10
|
+
* Override base message.
|
|
9
11
|
*/
|
|
10
|
-
|
|
12
|
+
message?: string;
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Structured details about the error.
|
|
@@ -21,56 +23,61 @@ export type BaseErrorOptions = {
|
|
|
21
23
|
export class BaseError<Code extends string = string> extends Error {
|
|
22
24
|
/**
|
|
23
25
|
* Primary way of defining new error classes.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* ```ts
|
|
28
|
-
* export class AiInputPreprocessingError extends BaseError.extend('AI_INPUT_PREPROCESSING_ERROR') {}
|
|
29
|
-
* ```
|
|
26
|
+
* Extended class may specialize constructor for required context params.
|
|
27
|
+
* @param code - Error code.
|
|
28
|
+
* @param message - Default error message.
|
|
30
29
|
*/
|
|
31
|
-
static extend<Code extends string>(code: Code) {
|
|
32
|
-
return class extends BaseError<Code> {
|
|
30
|
+
static extend<Code extends string = string>(code: Code, message?: string) {
|
|
31
|
+
return class ExtendedError extends BaseError<Code> {
|
|
33
32
|
static code = code;
|
|
34
33
|
|
|
35
34
|
static is(error: unknown): error is BaseError {
|
|
36
35
|
return typeof error === 'object' && error !== null && 'code' in error && error.code === code;
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
static wrap(
|
|
40
|
-
|
|
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;
|
|
41
50
|
}
|
|
42
51
|
|
|
43
|
-
constructor(
|
|
44
|
-
super(code, message, options);
|
|
52
|
+
constructor(options?: BaseErrorOptions) {
|
|
53
|
+
super(code, { message: options?.message ?? message, ...options });
|
|
45
54
|
}
|
|
46
55
|
};
|
|
47
56
|
}
|
|
48
57
|
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
// NOTE: Errors go through odd transformations and the private fields seem to break.
|
|
59
|
+
code: Code;
|
|
60
|
+
context: Record<string, unknown>;
|
|
51
61
|
|
|
52
|
-
constructor(code: Code,
|
|
53
|
-
super(message, options);
|
|
62
|
+
constructor(code: Code, options?: BaseErrorOptions) {
|
|
63
|
+
super(options?.message, { cause: options?.cause });
|
|
54
64
|
|
|
55
|
-
this
|
|
56
|
-
this
|
|
65
|
+
this.code = code;
|
|
66
|
+
this.context = options?.context ?? {};
|
|
57
67
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
58
68
|
}
|
|
59
69
|
|
|
60
70
|
override get name() {
|
|
61
|
-
return this
|
|
71
|
+
return this.code;
|
|
62
72
|
}
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
/** Fallback message. */
|
|
75
|
+
override get message() {
|
|
76
|
+
return this.constructor.name;
|
|
66
77
|
}
|
|
67
78
|
|
|
68
79
|
// For effect error matching.
|
|
69
80
|
get _tag(): Code {
|
|
70
|
-
return this
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get context() {
|
|
74
|
-
return this.#context;
|
|
81
|
+
return this.code;
|
|
75
82
|
}
|
|
76
83
|
}
|
package/src/errors.test.ts
CHANGED
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
+
import { BaseError, type BaseErrorOptions } from './base';
|
|
7
8
|
import { SystemError } from './errors';
|
|
8
9
|
|
|
9
10
|
describe('errors', () => {
|
|
10
11
|
test('error code and message, cause', () => {
|
|
11
|
-
const error = new SystemError('Test message',
|
|
12
|
+
const error = new SystemError({ message: 'Test message', cause: new Error('Test cause'), context: { a: 1, b: 2 } });
|
|
12
13
|
expect(error).toBeInstanceOf(SystemError);
|
|
13
14
|
expect(error).toBeInstanceOf(SystemError);
|
|
14
15
|
expect(error.code).toBe(SystemError.code);
|
|
@@ -24,19 +25,37 @@ describe('errors', () => {
|
|
|
24
25
|
expect.fail('Expected error to be thrown');
|
|
25
26
|
} catch (error: any) {
|
|
26
27
|
expect(error).toBeInstanceOf(SystemError);
|
|
27
|
-
expect(String(error)).toEqual('
|
|
28
|
+
expect(String(error)).toEqual('SYSTEM: Test message');
|
|
28
29
|
const stackLines = error.stack!.split('\n');
|
|
29
|
-
expect(stackLines?.[0]).toEqual('
|
|
30
|
+
expect(stackLines?.[0]).toEqual('SYSTEM: Test message');
|
|
30
31
|
expect(stackLines?.[1]).toMatch(/^ {4}at two \(.*$/);
|
|
31
32
|
expect(stackLines?.[2]).toMatch(/^ {4}at one \(.*$/);
|
|
32
33
|
}
|
|
33
34
|
});
|
|
35
|
+
|
|
36
|
+
test('custom message', () => {
|
|
37
|
+
class CustomError extends BaseError.extend('CUSTOM', 'Custom message') {
|
|
38
|
+
constructor(value: number, options?: Omit<BaseErrorOptions, 'context'>) {
|
|
39
|
+
super({ context: { value }, ...options });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const error = new CustomError(1);
|
|
44
|
+
expect(error).toBeInstanceOf(CustomError);
|
|
45
|
+
expect(error.message).toBe('Custom message');
|
|
46
|
+
expect(error.context).toEqual({ value: 1 });
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('is', () => {
|
|
50
|
+
const error = new SystemError({ message: 'Test message' });
|
|
51
|
+
expect(SystemError.is(error)).toBe(true);
|
|
52
|
+
});
|
|
34
53
|
});
|
|
35
54
|
|
|
36
55
|
const throwError = () => {
|
|
37
56
|
const one = () => {
|
|
38
57
|
const two = () => {
|
|
39
|
-
throw new SystemError('Test message');
|
|
58
|
+
throw new SystemError({ message: 'Test message' });
|
|
40
59
|
};
|
|
41
60
|
two();
|
|
42
61
|
};
|
package/src/errors.ts
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
import { BaseError } from './base';
|
|
6
6
|
|
|
7
|
-
export class
|
|
7
|
+
export class ApiError extends BaseError.extend('API', 'API error') {}
|
|
8
8
|
|
|
9
|
-
export class
|
|
9
|
+
export class SystemError extends BaseError.extend('SYSTEM', 'System error') {}
|
|
10
10
|
|
|
11
|
-
export class
|
|
11
|
+
export class InternalError extends BaseError.extend('INTERNAL', 'Internal error') {}
|
|
12
12
|
|
|
13
|
-
export class
|
|
13
|
+
export class TimeoutError extends BaseError.extend('TIMEOUT', 'Timeout') {}
|
|
14
14
|
|
|
15
|
-
export class
|
|
15
|
+
export class AbortedError extends BaseError.extend('ABORTED', 'Aborted') {}
|
|
16
16
|
|
|
17
|
-
export class
|
|
17
|
+
export class NotImplementedError extends BaseError.extend('NOT_IMPLEMENTED', 'Not implemented') {}
|