@dxos/errors 0.8.3 → 0.8.4-main.1da679c

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.
@@ -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', { 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
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('SYSTEM_ERROR: Test message');
28
+ expect(String(error)).toEqual('SYSTEM: Test message');
28
29
  const stackLines = error.stack!.split('\n');
29
- expect(stackLines?.[0]).toEqual('SYSTEM_ERROR: Test message');
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 TimeoutError extends BaseError.extend('TIMEOUT') {}
7
+ export class ApiError extends BaseError.extend('API', 'API error') {}
8
8
 
9
- export class AbortedError extends BaseError.extend('ABORTED') {}
9
+ export class SystemError extends BaseError.extend('SYSTEM', 'System error') {}
10
10
 
11
- export class UnimplementedError extends BaseError.extend('UNIMPLEMENTED') {}
11
+ export class InternalError extends BaseError.extend('INTERNAL', 'Internal error') {}
12
12
 
13
- export class ApiError extends BaseError.extend('API_ERROR') {}
13
+ export class TimeoutError extends BaseError.extend('TIMEOUT', 'Timeout') {}
14
14
 
15
- export class SystemError extends BaseError.extend('SYSTEM_ERROR') {}
15
+ export class AbortedError extends BaseError.extend('ABORTED', 'Aborted') {}
16
16
 
17
- export class InternalError extends BaseError.extend('INTERNAL_ERROR') {}
17
+ export class NotImplementedError extends BaseError.extend('NOT_IMPLEMENTED', 'Not implemented') {}
@@ -1,84 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var node_exports = {};
20
- __export(node_exports, {
21
- AbortedError: () => AbortedError,
22
- ApiError: () => ApiError,
23
- BaseError: () => BaseError,
24
- InternalError: () => InternalError,
25
- SystemError: () => SystemError,
26
- TimeoutError: () => TimeoutError,
27
- UnimplementedError: () => UnimplementedError
28
- });
29
- module.exports = __toCommonJS(node_exports);
30
- var BaseError = class _BaseError extends Error {
31
- static extend(code) {
32
- return class extends _BaseError {
33
- static {
34
- this.code = code;
35
- }
36
- static is(error) {
37
- return typeof error === "object" && error !== null && "code" in error && error.code === code;
38
- }
39
- constructor(message, options) {
40
- super(code, message, options);
41
- }
42
- };
43
- }
44
- #code;
45
- #context;
46
- constructor(code, message, options) {
47
- super(message, options);
48
- this.#code = code;
49
- this.#context = options?.context ?? {};
50
- Object.setPrototypeOf(this, new.target.prototype);
51
- }
52
- get name() {
53
- return this.#code;
54
- }
55
- get code() {
56
- return this.#code;
57
- }
58
- get context() {
59
- return this.#context;
60
- }
61
- };
62
- var TimeoutError = class extends BaseError.extend("TIMEOUT") {
63
- };
64
- var AbortedError = class extends BaseError.extend("ABORTED") {
65
- };
66
- var UnimplementedError = class extends BaseError.extend("UNIMPLEMENTED") {
67
- };
68
- var ApiError = class extends BaseError.extend("API_ERROR") {
69
- };
70
- var SystemError = class extends BaseError.extend("SYSTEM_ERROR") {
71
- };
72
- var InternalError = class extends BaseError.extend("INTERNAL_ERROR") {
73
- };
74
- // Annotate the CommonJS export names for ESM import in node:
75
- 0 && (module.exports = {
76
- AbortedError,
77
- ApiError,
78
- BaseError,
79
- InternalError,
80
- SystemError,
81
- TimeoutError,
82
- UnimplementedError
83
- });
84
- //# sourceMappingURL=index.cjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/base.ts", "../../../src/errors.ts"],
4
- "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nexport type BaseErrorOptions = {\n /**\n * The cause of the error.\n * An instance of Error.\n */\n cause?: unknown;\n\n /**\n * Structured details about the error.\n */\n context?: Record<string, unknown>;\n};\n\nexport class BaseError extends Error {\n static extend(code: string) {\n return class extends BaseError {\n static code = code;\n\n static is(error: unknown): error is BaseError {\n return typeof error === 'object' && error !== null && 'code' in error && error.code === code;\n }\n\n constructor(message: string, options?: BaseErrorOptions) {\n super(code, message, options);\n }\n };\n }\n\n #code: string;\n #context: Record<string, unknown>;\n\n constructor(code: string, message: string, options?: BaseErrorOptions) {\n super(message, options);\n\n this.#code = code;\n this.#context = options?.context ?? {};\n Object.setPrototypeOf(this, new.target.prototype);\n }\n\n override get name() {\n return this.#code;\n }\n\n get code() {\n return this.#code;\n }\n\n get context() {\n return this.#context;\n }\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { BaseError } from './base';\n\nexport class TimeoutError extends BaseError.extend('TIMEOUT') {}\n\nexport class AbortedError extends BaseError.extend('ABORTED') {}\n\nexport class UnimplementedError extends BaseError.extend('UNIMPLEMENTED') {}\n\nexport class ApiError extends BaseError.extend('API_ERROR') {}\n\nexport class SystemError extends BaseError.extend('SYSTEM_ERROR') {}\n\nexport class InternalError extends BaseError.extend('INTERNAL_ERROR') {}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBO,IAAMA,YAAN,MAAMA,mBAAkBC,MAAAA;EAC7B,OAAOC,OAAOC,MAAc;AAC1B,WAAO,cAAcH,WAAAA;MACnB,OAAA;aAAOG,OAAOA;;MAEd,OAAOC,GAAGC,OAAoC;AAC5C,eAAO,OAAOA,UAAU,YAAYA,UAAU,QAAQ,UAAUA,SAASA,MAAMF,SAASA;MAC1F;MAEA,YAAYG,SAAiBC,SAA4B;AACvD,cAAMJ,MAAMG,SAASC,OAAAA;MACvB;IACF;EACF;EAEA;EACA;EAEA,YAAYJ,MAAcG,SAAiBC,SAA4B;AACrE,UAAMD,SAASC,OAAAA;AAEf,SAAK,QAAQJ;AACb,SAAK,WAAWI,SAASC,WAAW,CAAC;AACrCC,WAAOC,eAAe,MAAM,WAAWC,SAAS;EAClD;EAEA,IAAaC,OAAO;AAClB,WAAO,KAAK;EACd;EAEA,IAAIT,OAAO;AACT,WAAO,KAAK;EACd;EAEA,IAAIK,UAAU;AACZ,WAAO,KAAK;EACd;AACF;AChDO,IAAMK,eAAN,cAA2Bb,UAAUE,OAAO,SAAA,EAAA;AAAY;AAExD,IAAMY,eAAN,cAA2Bd,UAAUE,OAAO,SAAA,EAAA;AAAY;AAExD,IAAMa,qBAAN,cAAiCf,UAAUE,OAAO,eAAA,EAAA;AAAkB;AAEpE,IAAMc,WAAN,cAAuBhB,UAAUE,OAAO,WAAA,EAAA;AAAc;AAEtD,IAAMe,cAAN,cAA0BjB,UAAUE,OAAO,cAAA,EAAA;AAAiB;AAE5D,IAAMgB,gBAAN,cAA4BlB,UAAUE,OAAO,gBAAA,EAAA;AAAmB;",
6
- "names": ["BaseError", "Error", "extend", "code", "is", "error", "message", "options", "context", "Object", "setPrototypeOf", "prototype", "name", "TimeoutError", "AbortedError", "UnimplementedError", "ApiError", "SystemError", "InternalError"]
7
- }
@@ -1 +0,0 @@
1
- {"inputs":{"packages/common/errors/src/base.ts":{"bytes":3744,"imports":[],"format":"esm"},"packages/common/errors/src/errors.ts":{"bytes":2013,"imports":[{"path":"packages/common/errors/src/base.ts","kind":"import-statement","original":"./base"}],"format":"esm"},"packages/common/errors/src/index.ts":{"bytes":536,"imports":[{"path":"packages/common/errors/src/base.ts","kind":"import-statement","original":"./base"},{"path":"packages/common/errors/src/errors.ts","kind":"import-statement","original":"./errors"}],"format":"esm"}},"outputs":{"packages/common/errors/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":2906},"packages/common/errors/dist/lib/node/index.cjs":{"imports":[],"exports":["AbortedError","ApiError","BaseError","InternalError","SystemError","TimeoutError","UnimplementedError"],"entryPoint":"packages/common/errors/src/index.ts","inputs":{"packages/common/errors/src/base.ts":{"bytesInOutput":742},"packages/common/errors/src/index.ts":{"bytesInOutput":0},"packages/common/errors/src/errors.ts":{"bytesInOutput":418}},"bytes":1396}}}