@dxos/errors 0.8.4-main.67995b8 → 0.8.4-main.69d29f4

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/package.json CHANGED
@@ -1,19 +1,23 @@
1
1
  {
2
2
  "name": "@dxos/errors",
3
- "version": "0.8.4-main.67995b8",
3
+ "version": "0.8.4-main.69d29f4",
4
4
  "description": "Error definitions",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
17
+ "source": "./src/index.ts",
13
18
  "types": "./dist/types/src/index.d.ts",
14
19
  "browser": "./dist/lib/browser/index.mjs",
15
- "node": "./dist/lib/node-esm/index.mjs",
16
- "source": "./src/index.ts"
20
+ "node": "./dist/lib/node-esm/index.mjs"
17
21
  }
18
22
  },
19
23
  "types": "dist/types/src/index.d.ts",
package/src/base.ts CHANGED
@@ -2,12 +2,14 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export type BaseErrorOptions = {
5
+ /**
6
+ * Options for creating a BaseError.
7
+ */
8
+ export type BaseErrorOptions = ErrorOptions & {
6
9
  /**
7
- * The cause of the error.
8
- * An instance of Error.
10
+ * Override base message.
9
11
  */
10
- cause?: unknown;
12
+ message?: string;
11
13
 
12
14
  /**
13
15
  * Structured details about the error.
@@ -18,59 +20,60 @@ export type BaseErrorOptions = {
18
20
  /**
19
21
  * Base class for all DXOS errors.
20
22
  */
21
- export class BaseError<Code extends string = string> extends Error {
23
+ export class BaseError<Name extends string = string> extends Error {
22
24
  /**
23
25
  * Primary way of defining new error classes.
24
- *
25
- * Expample:
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 name - Error name.
28
+ * @param message - Default error message.
30
29
  */
31
- static extend<Code extends string>(code: Code) {
32
- return class extends BaseError<Code> {
33
- static code = code;
30
+ static extend<Name extends string = string>(name: Name, message?: string) {
31
+ return class ExtendedError extends BaseError<Name> {
32
+ static override name: Name = name;
34
33
 
35
34
  static is(error: unknown): error is BaseError {
36
- return typeof error === 'object' && error !== null && 'code' in error && error.code === code;
35
+ return typeof error === 'object' && error !== null && 'name' in error && error.name === name;
37
36
  }
38
37
 
39
- static wrap(message: string, options?: Omit<BaseErrorOptions, 'cause'>) {
40
- return (error: unknown) => new this(message, { ...options, cause: error });
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(message: string, options?: BaseErrorOptions) {
44
- super(code, message, options);
52
+ constructor(options?: BaseErrorOptions) {
53
+ super(name, { message: options?.message ?? message, ...options });
45
54
  }
46
55
  };
47
56
  }
48
57
 
49
- #code: Code;
50
- #context: Record<string, unknown>;
58
+ // NOTE: Errors go through odd transformations and the private fields seem to break.
59
+ override name: Name;
60
+ context: Record<string, unknown>;
51
61
 
52
- constructor(code: Code, message: string, options?: BaseErrorOptions) {
53
- super(message, options);
62
+ constructor(name: Name, options?: BaseErrorOptions) {
63
+ super(options?.message, { cause: options?.cause });
54
64
 
55
- this.#code = code;
56
- this.#context = options?.context ?? {};
65
+ this.name = name;
66
+ this.context = options?.context ?? {};
57
67
  Object.setPrototypeOf(this, new.target.prototype);
58
68
  }
59
69
 
60
- override get name() {
61
- return this.#code;
62
- }
63
-
64
- get code(): Code {
65
- return this.#code;
70
+ /** Fallback message. */
71
+ override get message() {
72
+ return this.constructor.name;
66
73
  }
67
74
 
68
75
  // For effect error matching.
69
- get _tag(): Code {
70
- return this.#code;
71
- }
72
-
73
- get context() {
74
- return this.#context;
76
+ get _tag(): Name {
77
+ return this.name;
75
78
  }
76
79
  }
@@ -4,14 +4,16 @@
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', { cause: new Error('Test cause'), context: { a: 1, b: 2 } });
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
- expect(error.code).toBe(SystemError.code);
15
+ expect(error.name).toBe(SystemError.name);
16
+ expect(error._tag).toBe(SystemError.name);
15
17
  expect(error.message).toBe('Test message');
16
18
  expect(error.cause).toBeInstanceOf(Error);
17
19
  expect((error.cause as Error).message).toBe('Test cause');
@@ -24,19 +26,37 @@ describe('errors', () => {
24
26
  expect.fail('Expected error to be thrown');
25
27
  } catch (error: any) {
26
28
  expect(error).toBeInstanceOf(SystemError);
27
- expect(String(error)).toEqual('SYSTEM_ERROR: Test message');
29
+ expect(String(error)).toEqual('SystemError: Test message');
28
30
  const stackLines = error.stack!.split('\n');
29
- expect(stackLines?.[0]).toEqual('SYSTEM_ERROR: Test message');
31
+ expect(stackLines?.[0]).toEqual('SystemError: Test message');
30
32
  expect(stackLines?.[1]).toMatch(/^ {4}at two \(.*$/);
31
33
  expect(stackLines?.[2]).toMatch(/^ {4}at one \(.*$/);
32
34
  }
33
35
  });
36
+
37
+ test('custom message', () => {
38
+ class CustomError extends BaseError.extend('CustomError', 'Custom message') {
39
+ constructor(value: number, options?: Omit<BaseErrorOptions, 'context'>) {
40
+ super({ context: { value }, ...options });
41
+ }
42
+ }
43
+
44
+ const error = new CustomError(1);
45
+ expect(error).toBeInstanceOf(CustomError);
46
+ expect(error.message).toBe('Custom message');
47
+ expect(error.context).toEqual({ value: 1 });
48
+ });
49
+
50
+ test('is', () => {
51
+ const error = new SystemError({ message: 'Test message' });
52
+ expect(SystemError.is(error)).toBe(true);
53
+ });
34
54
  });
35
55
 
36
56
  const throwError = () => {
37
57
  const one = () => {
38
58
  const two = () => {
39
- throw new SystemError('Test message');
59
+ throw new SystemError({ message: 'Test message' });
40
60
  };
41
61
  two();
42
62
  };
package/src/errors.ts CHANGED
@@ -4,14 +4,16 @@
4
4
 
5
5
  import { BaseError } from './base';
6
6
 
7
- export class TimeoutError extends BaseError.extend('TIMEOUT') {}
7
+ export class ApiError extends BaseError.extend('ApiError', 'API error') {}
8
8
 
9
- export class AbortedError extends BaseError.extend('ABORTED') {}
9
+ export class SystemError extends BaseError.extend('SystemError', 'System error') {}
10
10
 
11
- export class UnimplementedError extends BaseError.extend('UNIMPLEMENTED') {}
11
+ export class InternalError extends BaseError.extend('InternalError', 'Internal error') {}
12
12
 
13
- export class ApiError extends BaseError.extend('API_ERROR') {}
13
+ export class TimeoutError extends BaseError.extend('TimeoutError', 'Timeout') {}
14
14
 
15
- export class SystemError extends BaseError.extend('SYSTEM_ERROR') {}
15
+ export class AbortedError extends BaseError.extend('AbortedError', 'Aborted') {}
16
16
 
17
- export class InternalError extends BaseError.extend('INTERNAL_ERROR') {}
17
+ export class NotImplementedError extends BaseError.extend('NotImplementedError', 'Not implemented') {}
18
+
19
+ export class RuntimeServiceError extends BaseError.extend('RuntimeServiceError', 'Runtime service error') {}