@dxos/errors 0.8.4-main.f9ba587 → 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.
@@ -1,57 +1,84 @@
1
1
  // src/base.ts
2
2
  var BaseError = class _BaseError extends Error {
3
- static extend(code) {
4
- return class extends _BaseError {
5
- static {
6
- this.code = code;
7
- }
3
+ /**
4
+ * Primary way of defining new error classes.
5
+ * Extended class may specialize constructor for required context params.
6
+ * @param code - Error code.
7
+ * @param message - Default error message.
8
+ */
9
+ static extend(code, message) {
10
+ return class ExtendedError extends _BaseError {
11
+ static code = code;
8
12
  static is(error) {
9
13
  return typeof error === "object" && error !== null && "code" in error && error.code === code;
10
14
  }
11
- constructor(message, options) {
12
- super(code, message, options);
15
+ static wrap(options) {
16
+ const wrapFn = (error) => {
17
+ if (options?.ifTypeDiffers === true && this.is(error)) {
18
+ return error;
19
+ }
20
+ const newError = new this({
21
+ message,
22
+ ...options,
23
+ cause: error
24
+ });
25
+ Error.captureStackTrace(newError, wrapFn);
26
+ return newError;
27
+ };
28
+ return wrapFn;
29
+ }
30
+ constructor(options) {
31
+ super(code, {
32
+ message: options?.message ?? message,
33
+ ...options
34
+ });
13
35
  }
14
36
  };
15
37
  }
16
- #code;
17
- #context;
18
- constructor(code, message, options) {
19
- super(message, options);
20
- this.#code = code;
21
- this.#context = options?.context ?? {};
38
+ // NOTE: Errors go through odd transformations and the private fields seem to break.
39
+ code;
40
+ context;
41
+ constructor(code, options) {
42
+ super(options?.message, {
43
+ cause: options?.cause
44
+ });
45
+ this.code = code;
46
+ this.context = options?.context ?? {};
22
47
  Object.setPrototypeOf(this, new.target.prototype);
23
48
  }
24
49
  get name() {
25
- return this.#code;
50
+ return this.code;
26
51
  }
27
- get code() {
28
- return this.#code;
52
+ /** Fallback message. */
53
+ get message() {
54
+ return this.constructor.name;
29
55
  }
30
- get context() {
31
- return this.#context;
56
+ // For effect error matching.
57
+ get _tag() {
58
+ return this.code;
32
59
  }
33
60
  };
34
61
 
35
62
  // src/errors.ts
36
- var TimeoutError = class extends BaseError.extend("TIMEOUT") {
63
+ var ApiError = class extends BaseError.extend("API", "API error") {
37
64
  };
38
- var AbortedError = class extends BaseError.extend("ABORTED") {
65
+ var SystemError = class extends BaseError.extend("SYSTEM", "System error") {
39
66
  };
40
- var UnimplementedError = class extends BaseError.extend("UNIMPLEMENTED") {
67
+ var InternalError = class extends BaseError.extend("INTERNAL", "Internal error") {
41
68
  };
42
- var ApiError = class extends BaseError.extend("API_ERROR") {
69
+ var TimeoutError = class extends BaseError.extend("TIMEOUT", "Timeout") {
43
70
  };
44
- var SystemError = class extends BaseError.extend("SYSTEM_ERROR") {
71
+ var AbortedError = class extends BaseError.extend("ABORTED", "Aborted") {
45
72
  };
46
- var InternalError = class extends BaseError.extend("INTERNAL_ERROR") {
73
+ var NotImplementedError = class extends BaseError.extend("NOT_IMPLEMENTED", "Not implemented") {
47
74
  };
48
75
  export {
49
76
  AbortedError,
50
77
  ApiError,
51
78
  BaseError,
52
79
  InternalError,
80
+ NotImplementedError,
53
81
  SystemError,
54
- TimeoutError,
55
- UnimplementedError
82
+ TimeoutError
56
83
  };
57
84
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;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,cAA2BC,UAAUC,OAAO,SAAA,EAAA;AAAY;AAExD,IAAMC,eAAN,cAA2BF,UAAUC,OAAO,SAAA,EAAA;AAAY;AAExD,IAAME,qBAAN,cAAiCH,UAAUC,OAAO,eAAA,EAAA;AAAkB;AAEpE,IAAMG,WAAN,cAAuBJ,UAAUC,OAAO,WAAA,EAAA;AAAc;AAEtD,IAAMI,cAAN,cAA0BL,UAAUC,OAAO,cAAA,EAAA;AAAiB;AAE5D,IAAMK,gBAAN,cAA4BN,UAAUC,OAAO,gBAAA,EAAA;AAAmB;",
6
- "names": ["BaseError", "Error", "extend", "code", "is", "error", "message", "options", "context", "Object", "setPrototypeOf", "prototype", "name", "TimeoutError", "BaseError", "extend", "AbortedError", "UnimplementedError", "ApiError", "SystemError", "InternalError"]
4
+ "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\n/**\n * Options for creating a BaseError.\n */\nexport type BaseErrorOptions = ErrorOptions & {\n /**\n * Override base message.\n */\n message?: string;\n\n /**\n * Structured details about the error.\n */\n context?: Record<string, unknown>;\n};\n\n/**\n * Base class for all DXOS errors.\n */\nexport class BaseError<Code extends string = string> extends Error {\n /**\n * Primary way of defining new error classes.\n * Extended class may specialize constructor for required context params.\n * @param code - Error code.\n * @param message - Default error message.\n */\n static extend<Code extends string = string>(code: Code, message?: string) {\n return class ExtendedError extends BaseError<Code> {\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 static wrap(\n options?: Omit<BaseErrorOptions, 'cause'> & { ifTypeDiffers?: boolean },\n ): (error: unknown) => ExtendedError {\n const wrapFn = (error: unknown) => {\n if (options?.ifTypeDiffers === true && this.is(error)) {\n return error as ExtendedError;\n }\n const newError: ExtendedError = new this({ message, ...options, cause: error });\n Error.captureStackTrace(newError, wrapFn); // Position stack-trace to start from the caller of `wrap`.\n return newError;\n };\n return wrapFn;\n }\n\n constructor(options?: BaseErrorOptions) {\n super(code, { message: options?.message ?? message, ...options });\n }\n };\n }\n\n // NOTE: Errors go through odd transformations and the private fields seem to break.\n code: Code;\n context: Record<string, unknown>;\n\n constructor(code: Code, options?: BaseErrorOptions) {\n super(options?.message, { cause: options?.cause });\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 /** Fallback message. */\n override get message() {\n return this.constructor.name;\n }\n\n // For effect error matching.\n get _tag(): Code {\n return this.code;\n }\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { BaseError } from './base';\n\nexport class ApiError extends BaseError.extend('API', 'API error') {}\n\nexport class SystemError extends BaseError.extend('SYSTEM', 'System error') {}\n\nexport class InternalError extends BaseError.extend('INTERNAL', 'Internal error') {}\n\nexport class TimeoutError extends BaseError.extend('TIMEOUT', 'Timeout') {}\n\nexport class AbortedError extends BaseError.extend('ABORTED', 'Aborted') {}\n\nexport class NotImplementedError extends BaseError.extend('NOT_IMPLEMENTED', 'Not implemented') {}\n"],
5
+ "mappings": ";AAsBO,IAAMA,YAAN,MAAMA,mBAAgDC,MAAAA;;;;;;;EAO3D,OAAOC,OAAqCC,MAAYC,SAAkB;AACxE,WAAO,MAAMC,sBAAsBL,WAAAA;MACjC,OAAOG,OAAOA;MAEd,OAAOG,GAAGC,OAAoC;AAC5C,eAAO,OAAOA,UAAU,YAAYA,UAAU,QAAQ,UAAUA,SAASA,MAAMJ,SAASA;MAC1F;MAEA,OAAOK,KACLC,SACmC;AACnC,cAAMC,SAAS,CAACH,UAAAA;AACd,cAAIE,SAASE,kBAAkB,QAAQ,KAAKL,GAAGC,KAAAA,GAAQ;AACrD,mBAAOA;UACT;AACA,gBAAMK,WAA0B,IAAI,KAAK;YAAER;YAAS,GAAGK;YAASI,OAAON;UAAM,CAAA;AAC7EN,gBAAMa,kBAAkBF,UAAUF,MAAAA;AAClC,iBAAOE;QACT;AACA,eAAOF;MACT;MAEA,YAAYD,SAA4B;AACtC,cAAMN,MAAM;UAAEC,SAASK,SAASL,WAAWA;UAAS,GAAGK;QAAQ,CAAA;MACjE;IACF;EACF;;EAGAN;EACAY;EAEA,YAAYZ,MAAYM,SAA4B;AAClD,UAAMA,SAASL,SAAS;MAAES,OAAOJ,SAASI;IAAM,CAAA;AAEhD,SAAKV,OAAOA;AACZ,SAAKY,UAAUN,SAASM,WAAW,CAAC;AACpCC,WAAOC,eAAe,MAAM,WAAWC,SAAS;EAClD;EAEA,IAAaC,OAAO;AAClB,WAAO,KAAKhB;EACd;;EAGA,IAAaC,UAAU;AACrB,WAAO,KAAK,YAAYe;EAC1B;;EAGA,IAAIC,OAAa;AACf,WAAO,KAAKjB;EACd;AACF;;;AC5EO,IAAMkB,WAAN,cAAuBC,UAAUC,OAAO,OAAO,WAAA,EAAA;AAAc;AAE7D,IAAMC,cAAN,cAA0BF,UAAUC,OAAO,UAAU,cAAA,EAAA;AAAiB;AAEtE,IAAME,gBAAN,cAA4BH,UAAUC,OAAO,YAAY,gBAAA,EAAA;AAAmB;AAE5E,IAAMG,eAAN,cAA2BJ,UAAUC,OAAO,WAAW,SAAA,EAAA;AAAY;AAEnE,IAAMI,eAAN,cAA2BL,UAAUC,OAAO,WAAW,SAAA,EAAA;AAAY;AAEnE,IAAMK,sBAAN,cAAkCN,UAAUC,OAAO,mBAAmB,iBAAA,EAAA;AAAoB;",
6
+ "names": ["BaseError", "Error", "extend", "code", "message", "ExtendedError", "is", "error", "wrap", "options", "wrapFn", "ifTypeDiffers", "newError", "cause", "captureStackTrace", "context", "Object", "setPrototypeOf", "prototype", "name", "_tag", "ApiError", "BaseError", "extend", "SystemError", "InternalError", "TimeoutError", "AbortedError", "NotImplementedError"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"src/base.ts":{"bytes":3731,"imports":[],"format":"esm"},"src/errors.ts":{"bytes":2000,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"}],"format":"esm"},"src/index.ts":{"bytes":523,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"},{"path":"src/errors.ts","kind":"import-statement","original":"./errors"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":2906},"dist/lib/browser/index.mjs":{"imports":[],"exports":["AbortedError","ApiError","BaseError","InternalError","SystemError","TimeoutError","UnimplementedError"],"entryPoint":"src/index.ts","inputs":{"src/base.ts":{"bytesInOutput":742},"src/index.ts":{"bytesInOutput":0},"src/errors.ts":{"bytesInOutput":418}},"bytes":1350}}}
1
+ {"inputs":{"src/base.ts":{"bytes":7183,"imports":[],"format":"esm"},"src/errors.ts":{"bytes":2213,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"}],"format":"esm"},"src/index.ts":{"bytes":523,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"},{"path":"src/errors.ts","kind":"import-statement","original":"./errors"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":4645},"dist/lib/browser/index.mjs":{"imports":[],"exports":["AbortedError","ApiError","BaseError","InternalError","NotImplementedError","SystemError","TimeoutError"],"entryPoint":"src/index.ts","inputs":{"src/base.ts":{"bytesInOutput":1594},"src/index.ts":{"bytesInOutput":0},"src/errors.ts":{"bytesInOutput":491}},"bytes":2276}}}
@@ -2,58 +2,85 @@ import { createRequire } from 'node:module';const require = createRequire(import
2
2
 
3
3
  // src/base.ts
4
4
  var BaseError = class _BaseError extends Error {
5
- static extend(code) {
6
- return class extends _BaseError {
7
- static {
8
- this.code = code;
9
- }
5
+ /**
6
+ * Primary way of defining new error classes.
7
+ * Extended class may specialize constructor for required context params.
8
+ * @param code - Error code.
9
+ * @param message - Default error message.
10
+ */
11
+ static extend(code, message) {
12
+ return class ExtendedError extends _BaseError {
13
+ static code = code;
10
14
  static is(error) {
11
15
  return typeof error === "object" && error !== null && "code" in error && error.code === code;
12
16
  }
13
- constructor(message, options) {
14
- super(code, message, options);
17
+ static wrap(options) {
18
+ const wrapFn = (error) => {
19
+ if (options?.ifTypeDiffers === true && this.is(error)) {
20
+ return error;
21
+ }
22
+ const newError = new this({
23
+ message,
24
+ ...options,
25
+ cause: error
26
+ });
27
+ Error.captureStackTrace(newError, wrapFn);
28
+ return newError;
29
+ };
30
+ return wrapFn;
31
+ }
32
+ constructor(options) {
33
+ super(code, {
34
+ message: options?.message ?? message,
35
+ ...options
36
+ });
15
37
  }
16
38
  };
17
39
  }
18
- #code;
19
- #context;
20
- constructor(code, message, options) {
21
- super(message, options);
22
- this.#code = code;
23
- this.#context = options?.context ?? {};
40
+ // NOTE: Errors go through odd transformations and the private fields seem to break.
41
+ code;
42
+ context;
43
+ constructor(code, options) {
44
+ super(options?.message, {
45
+ cause: options?.cause
46
+ });
47
+ this.code = code;
48
+ this.context = options?.context ?? {};
24
49
  Object.setPrototypeOf(this, new.target.prototype);
25
50
  }
26
51
  get name() {
27
- return this.#code;
52
+ return this.code;
28
53
  }
29
- get code() {
30
- return this.#code;
54
+ /** Fallback message. */
55
+ get message() {
56
+ return this.constructor.name;
31
57
  }
32
- get context() {
33
- return this.#context;
58
+ // For effect error matching.
59
+ get _tag() {
60
+ return this.code;
34
61
  }
35
62
  };
36
63
 
37
64
  // src/errors.ts
38
- var TimeoutError = class extends BaseError.extend("TIMEOUT") {
65
+ var ApiError = class extends BaseError.extend("API", "API error") {
39
66
  };
40
- var AbortedError = class extends BaseError.extend("ABORTED") {
67
+ var SystemError = class extends BaseError.extend("SYSTEM", "System error") {
41
68
  };
42
- var UnimplementedError = class extends BaseError.extend("UNIMPLEMENTED") {
69
+ var InternalError = class extends BaseError.extend("INTERNAL", "Internal error") {
43
70
  };
44
- var ApiError = class extends BaseError.extend("API_ERROR") {
71
+ var TimeoutError = class extends BaseError.extend("TIMEOUT", "Timeout") {
45
72
  };
46
- var SystemError = class extends BaseError.extend("SYSTEM_ERROR") {
73
+ var AbortedError = class extends BaseError.extend("ABORTED", "Aborted") {
47
74
  };
48
- var InternalError = class extends BaseError.extend("INTERNAL_ERROR") {
75
+ var NotImplementedError = class extends BaseError.extend("NOT_IMPLEMENTED", "Not implemented") {
49
76
  };
50
77
  export {
51
78
  AbortedError,
52
79
  ApiError,
53
80
  BaseError,
54
81
  InternalError,
82
+ NotImplementedError,
55
83
  SystemError,
56
- TimeoutError,
57
- UnimplementedError
84
+ TimeoutError
58
85
  };
59
86
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;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,cAA2BC,UAAUC,OAAO,SAAA,EAAA;AAAY;AAExD,IAAMC,eAAN,cAA2BF,UAAUC,OAAO,SAAA,EAAA;AAAY;AAExD,IAAME,qBAAN,cAAiCH,UAAUC,OAAO,eAAA,EAAA;AAAkB;AAEpE,IAAMG,WAAN,cAAuBJ,UAAUC,OAAO,WAAA,EAAA;AAAc;AAEtD,IAAMI,cAAN,cAA0BL,UAAUC,OAAO,cAAA,EAAA;AAAiB;AAE5D,IAAMK,gBAAN,cAA4BN,UAAUC,OAAO,gBAAA,EAAA;AAAmB;",
6
- "names": ["BaseError", "Error", "extend", "code", "is", "error", "message", "options", "context", "Object", "setPrototypeOf", "prototype", "name", "TimeoutError", "BaseError", "extend", "AbortedError", "UnimplementedError", "ApiError", "SystemError", "InternalError"]
4
+ "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\n/**\n * Options for creating a BaseError.\n */\nexport type BaseErrorOptions = ErrorOptions & {\n /**\n * Override base message.\n */\n message?: string;\n\n /**\n * Structured details about the error.\n */\n context?: Record<string, unknown>;\n};\n\n/**\n * Base class for all DXOS errors.\n */\nexport class BaseError<Code extends string = string> extends Error {\n /**\n * Primary way of defining new error classes.\n * Extended class may specialize constructor for required context params.\n * @param code - Error code.\n * @param message - Default error message.\n */\n static extend<Code extends string = string>(code: Code, message?: string) {\n return class ExtendedError extends BaseError<Code> {\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 static wrap(\n options?: Omit<BaseErrorOptions, 'cause'> & { ifTypeDiffers?: boolean },\n ): (error: unknown) => ExtendedError {\n const wrapFn = (error: unknown) => {\n if (options?.ifTypeDiffers === true && this.is(error)) {\n return error as ExtendedError;\n }\n const newError: ExtendedError = new this({ message, ...options, cause: error });\n Error.captureStackTrace(newError, wrapFn); // Position stack-trace to start from the caller of `wrap`.\n return newError;\n };\n return wrapFn;\n }\n\n constructor(options?: BaseErrorOptions) {\n super(code, { message: options?.message ?? message, ...options });\n }\n };\n }\n\n // NOTE: Errors go through odd transformations and the private fields seem to break.\n code: Code;\n context: Record<string, unknown>;\n\n constructor(code: Code, options?: BaseErrorOptions) {\n super(options?.message, { cause: options?.cause });\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 /** Fallback message. */\n override get message() {\n return this.constructor.name;\n }\n\n // For effect error matching.\n get _tag(): Code {\n return this.code;\n }\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { BaseError } from './base';\n\nexport class ApiError extends BaseError.extend('API', 'API error') {}\n\nexport class SystemError extends BaseError.extend('SYSTEM', 'System error') {}\n\nexport class InternalError extends BaseError.extend('INTERNAL', 'Internal error') {}\n\nexport class TimeoutError extends BaseError.extend('TIMEOUT', 'Timeout') {}\n\nexport class AbortedError extends BaseError.extend('ABORTED', 'Aborted') {}\n\nexport class NotImplementedError extends BaseError.extend('NOT_IMPLEMENTED', 'Not implemented') {}\n"],
5
+ "mappings": ";;;AAsBO,IAAMA,YAAN,MAAMA,mBAAgDC,MAAAA;;;;;;;EAO3D,OAAOC,OAAqCC,MAAYC,SAAkB;AACxE,WAAO,MAAMC,sBA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AAY;AAEnE,IAAMK,sBAAN,cAAkCN,UAAUC,OAAO,mBAAmB,iBAAA,EAAA;AAAoB;",
6
+ "names": ["BaseError", "Error", "extend", "code", "message", "ExtendedError", "is", "error", "wrap", "options", "wrapFn", "ifTypeDiffers", "newError", "cause", "captureStackTrace", "context", "Object", "setPrototypeOf", "prototype", "name", "_tag", "ApiError", "BaseError", "extend", "SystemError", "InternalError", "TimeoutError", "AbortedError", "NotImplementedError"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"src/base.ts":{"bytes":3731,"imports":[],"format":"esm"},"src/errors.ts":{"bytes":2000,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"}],"format":"esm"},"src/index.ts":{"bytes":523,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"},{"path":"src/errors.ts","kind":"import-statement","original":"./errors"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":2908},"dist/lib/node-esm/index.mjs":{"imports":[],"exports":["AbortedError","ApiError","BaseError","InternalError","SystemError","TimeoutError","UnimplementedError"],"entryPoint":"src/index.ts","inputs":{"src/base.ts":{"bytesInOutput":742},"src/index.ts":{"bytesInOutput":0},"src/errors.ts":{"bytesInOutput":418}},"bytes":1443}}}
1
+ {"inputs":{"src/base.ts":{"bytes":7183,"imports":[],"format":"esm"},"src/errors.ts":{"bytes":2213,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"}],"format":"esm"},"src/index.ts":{"bytes":523,"imports":[{"path":"src/base.ts","kind":"import-statement","original":"./base"},{"path":"src/errors.ts","kind":"import-statement","original":"./errors"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":4647},"dist/lib/node-esm/index.mjs":{"imports":[],"exports":["AbortedError","ApiError","BaseError","InternalError","NotImplementedError","SystemError","TimeoutError"],"entryPoint":"src/index.ts","inputs":{"src/base.ts":{"bytesInOutput":1594},"src/index.ts":{"bytesInOutput":0},"src/errors.ts":{"bytesInOutput":491}},"bytes":2369}}}
@@ -1,37 +1,63 @@
1
- export type BaseErrorOptions = {
1
+ /**
2
+ * Options for creating a BaseError.
3
+ */
4
+ export type BaseErrorOptions = ErrorOptions & {
2
5
  /**
3
- * The cause of the error.
4
- * An instance of Error.
6
+ * Override base message.
5
7
  */
6
- cause?: unknown;
8
+ message?: string;
7
9
  /**
8
10
  * Structured details about the error.
9
11
  */
10
12
  context?: Record<string, unknown>;
11
13
  };
12
- export declare class BaseError extends Error {
13
- #private;
14
- static extend(code: string): {
15
- new (message: string, options?: BaseErrorOptions): {
16
- "__#1@#code": string;
17
- "__#1@#context": Record<string, unknown>;
18
- readonly name: string;
19
- readonly code: string;
20
- readonly context: Record<string, unknown>;
21
- message: string;
14
+ /**
15
+ * Base class for all DXOS errors.
16
+ */
17
+ export declare class BaseError<Code extends string = string> extends Error {
18
+ /**
19
+ * Primary way of defining new error classes.
20
+ * Extended class may specialize constructor for required context params.
21
+ * @param code - Error code.
22
+ * @param message - Default error message.
23
+ */
24
+ static extend<Code extends string = string>(code: Code, message?: string): {
25
+ new (options?: BaseErrorOptions): {
26
+ code: Code;
27
+ context: Record<string, unknown>;
28
+ get name(): Code;
29
+ /** Fallback message. */
30
+ get message(): string;
31
+ get _tag(): Code;
22
32
  stack?: string;
23
33
  cause?: unknown;
24
34
  };
25
- code: string;
35
+ code: Code;
26
36
  is(error: unknown): error is BaseError;
27
- extend(code: string): /*elided*/ any;
37
+ wrap(options?: Omit<BaseErrorOptions, "cause"> & {
38
+ ifTypeDiffers?: boolean;
39
+ }): (error: unknown) => {
40
+ code: Code;
41
+ context: Record<string, unknown>;
42
+ get name(): Code;
43
+ /** Fallback message. */
44
+ get message(): string;
45
+ get _tag(): Code;
46
+ stack?: string;
47
+ cause?: unknown;
48
+ };
49
+ extend<Code extends string = string>(code: Code, message?: string): /*elided*/ any;
50
+ isError(error: unknown): error is Error;
28
51
  captureStackTrace(targetObject: object, constructorOpt?: Function): void;
29
52
  prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
30
53
  stackTraceLimit: number;
31
54
  };
32
- constructor(code: string, message: string, options?: BaseErrorOptions);
33
- get name(): string;
34
- get code(): string;
35
- get context(): Record<string, unknown>;
55
+ code: Code;
56
+ context: Record<string, unknown>;
57
+ constructor(code: Code, options?: BaseErrorOptions);
58
+ get name(): Code;
59
+ /** Fallback message. */
60
+ get message(): string;
61
+ get _tag(): Code;
36
62
  }
37
63
  //# sourceMappingURL=base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/base.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,CAAC;AAEF,qBAAa,SAAU,SAAQ,KAAK;;IAClC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM;sBAQD,MAAM,YAAY,gBAAgB;0BAMpD,MAAM;6BACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;;;;;;;;;kBAXZ,OAAO,GAAG,KAAK,IAAI,SAAS;qBAJ7B,MAAM;;;;;gBAiBd,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB;IAQrE,IAAa,IAAI,WAEhB;IAED,IAAI,IAAI,WAEP;IAED,IAAI,OAAO,4BAEV;CACF"}
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/base.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,YAAY,GAAG;IAC5C;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAA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