@jakubmazanec/error 1.0.1-next.8fca4dc → 2.0.0-9cfd7b2

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.
Files changed (48) hide show
  1. package/LICENSE.md +840 -0
  2. package/README.md +97 -0
  3. package/dist/constants.d.ts +3 -0
  4. package/dist/constants.js +4 -2
  5. package/dist/constants.js.map +1 -1
  6. package/dist/createCustomError.d.ts +8 -8
  7. package/dist/createCustomError.js +15 -15
  8. package/dist/createCustomError.js.map +1 -1
  9. package/dist/createCustomErrorWithData.d.ts +14 -0
  10. package/dist/createCustomErrorWithData.js +26 -0
  11. package/dist/createCustomErrorWithData.js.map +1 -0
  12. package/dist/internals/createErrorMessage.d.ts +10 -0
  13. package/dist/internals/createErrorMessage.js +17 -0
  14. package/dist/internals/createErrorMessage.js.map +1 -0
  15. package/dist/internals.d.ts +1 -0
  16. package/dist/internals.js +3 -0
  17. package/dist/internals.js.map +1 -0
  18. package/dist/main.d.ts +2 -0
  19. package/dist/main.js +4 -2
  20. package/dist/main.js.map +1 -1
  21. package/dist/types/CustomError.d.ts +2 -4
  22. package/dist/types/CustomError.js.map +1 -1
  23. package/dist/types/CustomErrorOptions.d.ts +9 -0
  24. package/dist/types/CustomErrorOptions.js +5 -0
  25. package/dist/types/CustomErrorOptions.js.map +1 -0
  26. package/dist/types/CustomErrorWithData.d.ts +10 -0
  27. package/dist/types/CustomErrorWithData.js +3 -0
  28. package/dist/types/CustomErrorWithData.js.map +1 -0
  29. package/dist/types/CustomErrorWithDataOptions.d.ts +11 -0
  30. package/dist/types/CustomErrorWithDataOptions.js +5 -0
  31. package/dist/types/CustomErrorWithDataOptions.js.map +1 -0
  32. package/dist/types/ErrorMessages.d.ts +1 -1
  33. package/dist/types.d.ts +3 -0
  34. package/dist/types.js +5 -2
  35. package/dist/types.js.map +1 -1
  36. package/package.json +9 -6
  37. package/src/constants.ts +3 -0
  38. package/src/createCustomError.ts +22 -47
  39. package/src/createCustomErrorWithData.ts +79 -0
  40. package/src/internals/createErrorMessage.ts +27 -0
  41. package/src/internals.ts +1 -0
  42. package/src/main.ts +2 -0
  43. package/src/types/CustomError.ts +2 -5
  44. package/src/types/CustomErrorOptions.ts +10 -0
  45. package/src/types/CustomErrorWithData.ts +12 -0
  46. package/src/types/CustomErrorWithDataOptions.ts +13 -0
  47. package/src/types.ts +3 -0
  48. package/LICENSE +0 -18
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ <div align="center">
2
+
3
+ # @jakubmazanec/error
4
+
5
+ Collection of utilities for creating and handling errors.
6
+
7
+ </div>
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ npm install @jakubmazanec/error
13
+ ```
14
+
15
+ ⚠️ This is an [ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) package!
16
+ It cannot be required from a CommonJS module.
17
+
18
+ #### Prerequisites
19
+
20
+ - Node.js 16 or later
21
+ - TypeScript 5 or later
22
+
23
+ ## Usage
24
+
25
+ Create custom error classes to distinguish between errors and to ensure consistent error messages:
26
+
27
+ ```TypeScript
28
+ import {createCustomError} from '@jakubmazanec/error';
29
+
30
+ let FoobarError = createCustomError('FoobarError', {
31
+ FOOBAR_NOT_FOUND: 'Foobar was not found!',
32
+ });
33
+
34
+ try {
35
+ throw new FoobarError('FOOBAR_NOT_FOUND');
36
+ } catch (error: unknown) {
37
+ console.log(error instanceof FoobarError); // -> true
38
+ console.log(error.message); // -> 'Foobar was not found!'
39
+ console.log(error.code); // -> 'FOOBAR_NOT_FOUND'
40
+ }
41
+ ```
42
+
43
+ You can also have arbitrary data attached:
44
+
45
+ ```TypeScript
46
+ import {createCustomErrorWithData} from '@jakubmazanec/error';
47
+ import {z} from 'zod';
48
+
49
+ let FoobarError = createCustomErrorWithData(
50
+ 'FoobarError',
51
+ {FOOBAR_FAILED: 'Foobar failed with exit code "{0}" and message "{1}"!'},
52
+ z.object({
53
+ cwd: z.string(),
54
+ })
55
+ );
56
+
57
+ function runFoobar() {
58
+ let foobarOptions = {
59
+ cwd: '/foobar',
60
+ };
61
+
62
+ try {
63
+ foobar(foobarOptions);
64
+ } catch (error: unknown) {
65
+ if (error instanceof Error) {
66
+ throw new FoobarError('FOOBAR_FAILED', {
67
+ messageParameters: [42, 'Oops :('],
68
+ data: foobarOptions,
69
+ cause: error,
70
+ });
71
+ }
72
+ }
73
+ }
74
+
75
+ try {
76
+ runFoobar();
77
+ } catch (error: unknown) {
78
+ console.log(error instanceof FoobarError); // -> true
79
+ console.log(error.message); // -> 'Foobar failed with exit code "42" and message "Oops :("!'
80
+ console.log(error.code); // -> 'FOOBAR_FAILED'
81
+ console.log(error.data); // -> { cwd: '/foobar' }
82
+ console.log(error.cause); // -> Error
83
+ }
84
+ ```
85
+
86
+ ## Documentation
87
+
88
+ See [API reference](./docs) for auto-generated documentation.
89
+
90
+ ## Contributing
91
+
92
+ If you want to contribute, see [CONTRIBUTING](./CONTRIBUTING.md) for details.
93
+
94
+ ## License
95
+
96
+ This package is licensed under the GNU Lesser General Public License v3. See [LICENSE](./LICENSE.md)
97
+ for details.
@@ -1,4 +1,7 @@
1
1
  export declare const ERROR_MESSAGE_TOKEN_PATTERN: RegExp;
2
+ /**
3
+ * Code for an unknown error. You should never use nor encounter this error code.
4
+ */
2
5
  export declare const UNKNOWN_ERROR = "UNKNOWN_ERROR";
3
6
  export declare const INTERNAL_ERROR_MESSAGES: {
4
7
  UNKNOWN_ERROR: string;
package/dist/constants.js CHANGED
@@ -1,7 +1,9 @@
1
1
  export const ERROR_MESSAGE_TOKEN_PATTERN = /\{(\d+)\}/gu;
2
- export const UNKNOWN_ERROR = "UNKNOWN_ERROR";
2
+ /**
3
+ * Code for an unknown error. You should never use nor encounter this error code.
4
+ */ export const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
3
5
  export const INTERNAL_ERROR_MESSAGES = {
4
- [UNKNOWN_ERROR]: "An unknown error has occurred."
6
+ [UNKNOWN_ERROR]: 'An unknown error has occurred.'
5
7
  };
6
8
 
7
9
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/constants.ts"],"sourcesContent":["export const ERROR_MESSAGE_TOKEN_PATTERN = /\\{(\\d+)\\}/gu;\n\nexport const UNKNOWN_ERROR = 'UNKNOWN_ERROR';\n\nexport const INTERNAL_ERROR_MESSAGES = {\n [UNKNOWN_ERROR]: 'An unknown error has occurred.',\n};\n"],"names":["ERROR_MESSAGE_TOKEN_PATTERN","UNKNOWN_ERROR","INTERNAL_ERROR_MESSAGES"],"mappings":"AAAA,OAAO,MAAMA,8BAA8B,cAAc;AAEzD,OAAO,MAAMC,gBAAgB,gBAAgB;AAE7C,OAAO,MAAMC,0BAA0B;IACrC,CAACD,cAAc,EAAE;AACnB,EAAE"}
1
+ {"version":3,"sources":["../src/constants.ts"],"sourcesContent":["export const ERROR_MESSAGE_TOKEN_PATTERN = /\\{(\\d+)\\}/gu;\n\n/**\n * Code for an unknown error. You should never use nor encounter this error code.\n */\nexport const UNKNOWN_ERROR = 'UNKNOWN_ERROR';\n\nexport const INTERNAL_ERROR_MESSAGES = {\n [UNKNOWN_ERROR]: 'An unknown error has occurred.',\n};\n"],"names":["ERROR_MESSAGE_TOKEN_PATTERN","UNKNOWN_ERROR","INTERNAL_ERROR_MESSAGES"],"mappings":"AAAA,OAAO,MAAMA,8BAA8B,cAAc;AAEzD;;CAEC,GACD,OAAO,MAAMC,gBAAgB,gBAAgB;AAE7C,OAAO,MAAMC,0BAA0B;IACrC,CAACD,cAAc,EAAE;AACnB,EAAE"}
@@ -1,11 +1,11 @@
1
1
  import { UNKNOWN_ERROR } from './constants.js';
2
- import type { CustomError, ErrorMessages } from './types.js';
3
- export interface CustomErrorOptions<T> {
4
- messageParameters?: unknown[];
5
- data: T;
6
- }
2
+ import { type CustomError, type CustomErrorOptions, type ErrorMessages } from './types.js';
7
3
  /**
8
- * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies
9
- * generating error messages via predefined error codes and allows attaching custom data to the error instance.
4
+ * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error` and simplifies
5
+ * generating error messages via predefined error codes.
6
+ *
7
+ * @param errorName Error name.
8
+ * @param errorMessages Object containing all possible error messages.
9
+ * @return Class that implements {@linkcode CustomError}.
10
10
  */
11
- export declare function createCustomError<ErrorCode extends string, ErrorData extends abstract new (...args: any) => any>(errorName: string, errorMessages: ErrorMessages<ErrorCode>, ErrorData?: ErrorData): new (code: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) => CustomError<ErrorCode | typeof UNKNOWN_ERROR, InstanceType<ErrorData>>;
11
+ export declare function createCustomError<ErrorCode extends string>(errorName: string, errorMessages: ErrorMessages<ErrorCode>): new (code: ErrorCode, options?: CustomErrorOptions) => CustomError<ErrorCode | typeof UNKNOWN_ERROR>;
@@ -1,23 +1,23 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any -- needed */ import { ERROR_MESSAGE_TOKEN_PATTERN, INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR } from "./constants.js";
2
- function createErrorMessage(errorCode, errorMessages, messageParameters = []) {
3
- let errorMessage = errorMessages[errorCode];
4
- if (!errorMessage) {
5
- return "";
6
- }
7
- return `${errorCode}: ${errorMessage.replace(ERROR_MESSAGE_TOKEN_PATTERN, (match, index)=>String(messageParameters[index]))}`;
8
- }
1
+ import { INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR } from './constants.js';
2
+ import { createErrorMessage } from './internals.js';
9
3
  /**
10
- * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies
11
- * generating error messages via predefined error codes and allows attaching custom data to the error instance.
12
- */ export function createCustomError(errorName, errorMessages, ErrorData) {
4
+ * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error` and simplifies
5
+ * generating error messages via predefined error codes.
6
+ *
7
+ * @param errorName Error name.
8
+ * @param errorMessages Object containing all possible error messages.
9
+ * @return Class that implements {@linkcode CustomError}.
10
+ */ export function createCustomError(errorName, errorMessages) {
13
11
  return class InternalCustomError extends Error {
14
12
  constructor(errorCode, options){
15
- super(createErrorMessage(errorCode, errorMessages, options === null || options === void 0 ? void 0 : options.messageParameters));
13
+ let errorMessage = createErrorMessage(errorCode, errorMessages, options === null || options === void 0 ? void 0 : options.messageParameters);
14
+ let errorOptions = {};
15
+ if (typeof (options === null || options === void 0 ? void 0 : options.cause) !== 'undefined') {
16
+ errorOptions.cause = options.cause;
17
+ }
18
+ super(errorMessage, errorOptions);
16
19
  this.code = errorCode;
17
20
  this.name = errorName;
18
- if (typeof (options === null || options === void 0 ? void 0 : options.data) !== "undefined") {
19
- this.data = options.data;
20
- }
21
21
  // if the message is empty, we are making this an unknown error
22
22
  if (!this.message) {
23
23
  this.code = UNKNOWN_ERROR;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/createCustomError.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {ERROR_MESSAGE_TOKEN_PATTERN, INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR} from './constants.js';\nimport type {CustomError, ErrorMessages} from './types.js';\n\nfunction createErrorMessage<T extends string>(\n errorCode: T,\n errorMessages: ErrorMessages<T>,\n messageParameters: unknown[] = [],\n): string {\n let errorMessage = errorMessages[errorCode];\n\n if (!errorMessage) {\n return '';\n }\n\n return `${errorCode}: ${errorMessage.replace(\n ERROR_MESSAGE_TOKEN_PATTERN,\n (match: string, index: number) => String(messageParameters[index]),\n )}`;\n}\n\nexport interface CustomErrorOptions<T> {\n messageParameters?: unknown[];\n data: T;\n}\n\n/**\n * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies\n * generating error messages via predefined error codes and allows attaching custom data to the error instance.\n */\nexport function createCustomError<\n ErrorCode extends string,\n // eslint-disable-next-line @typescript-eslint/no-redeclare -- because `ErrorData` is used only as a type, we don't mind\n ErrorData extends abstract new (...args: any) => any,\n>(\n errorName: string,\n errorMessages: ErrorMessages<ErrorCode>,\n ErrorData?: ErrorData,\n): new (code: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) => CustomError<\n ErrorCode | typeof UNKNOWN_ERROR,\n InstanceType<ErrorData>\n> {\n return class InternalCustomError\n extends Error\n implements CustomError<ErrorCode, InstanceType<ErrorData>>\n {\n code: ErrorCode | typeof UNKNOWN_ERROR;\n data?: InstanceType<ErrorData>;\n\n constructor(errorCode: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) {\n super(createErrorMessage(errorCode, errorMessages, options?.messageParameters));\n\n this.code = errorCode;\n this.name = errorName;\n\n if (typeof options?.data !== 'undefined') {\n this.data = options.data;\n }\n\n // if the message is empty, we are making this an unknown error\n if (!this.message) {\n this.code = UNKNOWN_ERROR;\n this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);\n }\n }\n };\n}\n"],"names":["ERROR_MESSAGE_TOKEN_PATTERN","INTERNAL_ERROR_MESSAGES","UNKNOWN_ERROR","createErrorMessage","errorCode","errorMessages","messageParameters","errorMessage","replace","match","index","String","createCustomError","errorName","ErrorData","InternalCustomError","Error","constructor","options","code","name","data","message"],"mappings":"AAAA,+DAA+D,GAC/D,SAAQA,2BAA2B,EAAEC,uBAAuB,EAAEC,aAAa,QAAO,iBAAiB;AAGnG,SAASC,mBACPC,SAAY,EACZC,aAA+B,EAC/BC,oBAA+B,EAAE,EACzB;IACR,IAAIC,eAAeF,aAAa,CAACD,UAAU;IAE3C,IAAI,CAACG,cAAc;QACjB,OAAO;IACT,CAAC;IAED,OAAO,CAAC,EAAEH,UAAU,EAAE,EAAEG,aAAaC,OAAO,CAC1CR,6BACA,CAACS,OAAeC,QAAkBC,OAAOL,iBAAiB,CAACI,MAAM,GACjE,CAAC;AACL;AAOA;;;CAGC,GACD,OAAO,SAASE,kBAKdC,SAAiB,EACjBR,aAAuC,EACvCS,SAAqB,EAIrB;IACA,OAAO,MAAMC,4BACHC;QAMRC,YAAYb,SAAoB,EAAEc,OAAqD,CAAE;YACvF,KAAK,CAACf,mBAAmBC,WAAWC,eAAea,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASZ,iBAAiB;YAE7E,IAAI,CAACa,IAAI,GAAGf;YACZ,IAAI,CAACgB,IAAI,GAAGP;YAEZ,IAAI,OAAOK,CAAAA,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASG,IAAI,AAAD,MAAM,aAAa;gBACxC,IAAI,CAACA,IAAI,GAAGH,QAAQG,IAAI;YAC1B,CAAC;YAED,+DAA+D;YAC/D,IAAI,CAAC,IAAI,CAACC,OAAO,EAAE;gBACjB,IAAI,CAACH,IAAI,GAAGjB;gBACZ,IAAI,CAACoB,OAAO,GAAGnB,mBAAmBD,eAAeD;YACnD,CAAC;QACH;IACF;AACF,CAAC"}
1
+ {"version":3,"sources":["../src/createCustomError.ts"],"sourcesContent":["import {INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR} from './constants.js';\nimport {createErrorMessage} from './internals.js';\nimport {type CustomError, type CustomErrorOptions, type ErrorMessages} from './types.js';\n\n/**\n * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error` and simplifies\n * generating error messages via predefined error codes.\n *\n * @param errorName Error name.\n * @param errorMessages Object containing all possible error messages.\n * @return Class that implements {@linkcode CustomError}.\n */\nexport function createCustomError<ErrorCode extends string>(\n errorName: string,\n errorMessages: ErrorMessages<ErrorCode>,\n): new (code: ErrorCode, options?: CustomErrorOptions) => CustomError<\n ErrorCode | typeof UNKNOWN_ERROR\n> {\n return class InternalCustomError extends Error implements CustomError<ErrorCode> {\n code: ErrorCode | typeof UNKNOWN_ERROR;\n\n constructor(errorCode: ErrorCode, options?: CustomErrorOptions) {\n let errorMessage = createErrorMessage(errorCode, errorMessages, options?.messageParameters);\n let errorOptions: ErrorOptions = {};\n\n if (typeof options?.cause !== 'undefined') {\n errorOptions.cause = options.cause;\n }\n\n super(errorMessage, errorOptions);\n\n this.code = errorCode;\n this.name = errorName;\n\n // if the message is empty, we are making this an unknown error\n if (!this.message) {\n this.code = UNKNOWN_ERROR;\n this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);\n }\n }\n };\n}\n"],"names":["INTERNAL_ERROR_MESSAGES","UNKNOWN_ERROR","createErrorMessage","createCustomError","errorName","errorMessages","InternalCustomError","Error","constructor","errorCode","options","errorMessage","messageParameters","errorOptions","cause","code","name","message"],"mappings":"AAAA,SAAQA,uBAAuB,EAAEC,aAAa,QAAO,iBAAiB;AACtE,SAAQC,kBAAkB,QAAO,iBAAiB;AAGlD;;;;;;;CAOC,GACD,OAAO,SAASC,kBACdC,SAAiB,EACjBC,aAAuC,EAGvC;IACA,OAAO,MAAMC,4BAA4BC;QAGvCC,YAAYC,SAAoB,EAAEC,OAA4B,CAAE;YAC9D,IAAIC,eAAeT,mBAAmBO,WAAWJ,eAAeK,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASE,iBAAiB;YAC1F,IAAIC,eAA6B,CAAC;YAElC,IAAI,OAAOH,CAAAA,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASI,KAAK,AAAD,MAAM,aAAa;gBACzCD,aAAaC,KAAK,GAAGJ,QAAQI,KAAK;YACpC,CAAC;YAED,KAAK,CAACH,cAAcE;YAEpB,IAAI,CAACE,IAAI,GAAGN;YACZ,IAAI,CAACO,IAAI,GAAGZ;YAEZ,+DAA+D;YAC/D,IAAI,CAAC,IAAI,CAACa,OAAO,EAAE;gBACjB,IAAI,CAACF,IAAI,GAAGd;gBACZ,IAAI,CAACgB,OAAO,GAAGf,mBAAmBD,eAAeD;YACnD,CAAC;QACH;IACF;AACF,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { type z, type AnyZodObject } from 'zod';
2
+ import { UNKNOWN_ERROR } from './constants.js';
3
+ import { type CustomErrorWithData, type CustomErrorWithDataOptions, type ErrorMessages } from './types.js';
4
+ /**
5
+ * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies
6
+ * generating error messages via predefined error codes and allows attaching custom data to the error instance.
7
+ *
8
+ * @param errorName Error name.
9
+ * @param errorMessages Object containing all possible error messages.
10
+ * @param errorData Runtime representation of the `ErrorData` type parameter; can be either a class expression, or a Zod schema.
11
+ * @return Class that implements {@linkcode CustomErrorWithData}.
12
+ */
13
+ export declare function createCustomErrorWithData<ErrorCode extends string, ErrorData extends AnyZodObject>(errorName: string, errorMessages: ErrorMessages<ErrorCode>, errorData: ErrorData): new (code: ErrorCode, options?: CustomErrorWithDataOptions<z.infer<ErrorData>>) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, z.infer<ErrorData>>;
14
+ export declare function createCustomErrorWithData<ErrorCode extends string, ErrorData extends abstract new (...args: any) => any>(errorName: string, errorMessages: ErrorMessages<ErrorCode>, errorData: ErrorData): new (code: ErrorCode, options?: CustomErrorWithDataOptions<InstanceType<ErrorData>>) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, InstanceType<ErrorData>>;
@@ -0,0 +1,26 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any -- needed */ import { INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR } from './constants.js';
2
+ import { createErrorMessage } from './internals.js';
3
+ export function createCustomErrorWithData(errorName, errorMessages, errorData) {
4
+ return class InternalCustomError extends Error {
5
+ constructor(errorCode, options){
6
+ let errorMessage = createErrorMessage(errorCode, errorMessages, options === null || options === void 0 ? void 0 : options.messageParameters);
7
+ let errorOptions = {};
8
+ if (typeof (options === null || options === void 0 ? void 0 : options.cause) !== 'undefined') {
9
+ errorOptions.cause = options.cause;
10
+ }
11
+ super(errorMessage, errorOptions);
12
+ this.code = errorCode;
13
+ this.name = errorName;
14
+ if (typeof (options === null || options === void 0 ? void 0 : options.data) !== 'undefined') {
15
+ this.data = options.data;
16
+ }
17
+ // if the message is empty, we are making this an unknown error
18
+ if (!this.message) {
19
+ this.code = UNKNOWN_ERROR;
20
+ this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);
21
+ }
22
+ }
23
+ };
24
+ }
25
+
26
+ //# sourceMappingURL=createCustomErrorWithData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/createCustomErrorWithData.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type z, type AnyZodObject} from 'zod';\n\nimport {INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR} from './constants.js';\nimport {createErrorMessage} from './internals.js';\nimport {\n type CustomErrorWithData,\n type CustomErrorWithDataOptions,\n type ErrorMessages,\n} from './types.js';\n\n/**\n * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies\n * generating error messages via predefined error codes and allows attaching custom data to the error instance.\n *\n * @param errorName Error name.\n * @param errorMessages Object containing all possible error messages.\n * @param errorData Runtime representation of the `ErrorData` type parameter; can be either a class expression, or a Zod schema.\n * @return Class that implements {@linkcode CustomErrorWithData}.\n */\nexport function createCustomErrorWithData<ErrorCode extends string, ErrorData extends AnyZodObject>(\n errorName: string,\n errorMessages: ErrorMessages<ErrorCode>,\n errorData: ErrorData,\n): new (\n code: ErrorCode,\n options?: CustomErrorWithDataOptions<z.infer<ErrorData>>,\n) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, z.infer<ErrorData>>;\nexport function createCustomErrorWithData<\n ErrorCode extends string,\n ErrorData extends abstract new (...args: any) => any,\n>(\n errorName: string,\n errorMessages: ErrorMessages<ErrorCode>,\n errorData: ErrorData,\n): new (\n code: ErrorCode,\n options?: CustomErrorWithDataOptions<InstanceType<ErrorData>>,\n) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, InstanceType<ErrorData>>;\nexport function createCustomErrorWithData<ErrorCode extends string, ErrorData>(\n errorName: string,\n errorMessages: ErrorMessages<ErrorCode>,\n errorData: ErrorData,\n): new (code: ErrorCode, options?: CustomErrorWithDataOptions<ErrorData>) => CustomErrorWithData<\n ErrorCode | typeof UNKNOWN_ERROR,\n ErrorData\n> {\n return class InternalCustomError\n extends Error\n implements CustomErrorWithData<ErrorCode, ErrorData>\n {\n code: ErrorCode | typeof UNKNOWN_ERROR;\n data?: ErrorData;\n\n constructor(errorCode: ErrorCode, options?: CustomErrorWithDataOptions<ErrorData>) {\n let errorMessage = createErrorMessage(errorCode, errorMessages, options?.messageParameters);\n let errorOptions: ErrorOptions = {};\n\n if (typeof options?.cause !== 'undefined') {\n errorOptions.cause = options.cause;\n }\n\n super(errorMessage, errorOptions);\n\n this.code = errorCode;\n this.name = errorName;\n\n if (typeof options?.data !== 'undefined') {\n this.data = options.data;\n }\n\n // if the message is empty, we are making this an unknown error\n if (!this.message) {\n this.code = UNKNOWN_ERROR;\n this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);\n }\n }\n };\n}\n"],"names":["INTERNAL_ERROR_MESSAGES","UNKNOWN_ERROR","createErrorMessage","createCustomErrorWithData","errorName","errorMessages","errorData","InternalCustomError","Error","constructor","errorCode","options","errorMessage","messageParameters","errorOptions","cause","code","name","data","message"],"mappings":"AAAA,+DAA+D,GAC/D,AAEA,SAAQA,uBAAuB,EAAEC,aAAa,QAAO,iBAAiB;AACtE,SAAQC,kBAAkB,QAAO,iBAAiB;AAmClD,OAAO,SAASC,0BACdC,SAAiB,EACjBC,aAAuC,EACvCC,SAAoB,EAIpB;IACA,OAAO,MAAMC,4BACHC;QAMRC,YAAYC,SAAoB,EAAEC,OAA+C,CAAE;YACjF,IAAIC,eAAeV,mBAAmBQ,WAAWL,eAAeM,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASE,iBAAiB;YAC1F,IAAIC,eAA6B,CAAC;YAElC,IAAI,OAAOH,CAAAA,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASI,KAAK,AAAD,MAAM,aAAa;gBACzCD,aAAaC,KAAK,GAAGJ,QAAQI,KAAK;YACpC,CAAC;YAED,KAAK,CAACH,cAAcE;YAEpB,IAAI,CAACE,IAAI,GAAGN;YACZ,IAAI,CAACO,IAAI,GAAGb;YAEZ,IAAI,OAAOO,CAAAA,oBAAAA,qBAAAA,KAAAA,IAAAA,QAASO,IAAI,AAAD,MAAM,aAAa;gBACxC,IAAI,CAACA,IAAI,GAAGP,QAAQO,IAAI;YAC1B,CAAC;YAED,+DAA+D;YAC/D,IAAI,CAAC,IAAI,CAACC,OAAO,EAAE;gBACjB,IAAI,CAACH,IAAI,GAAGf;gBACZ,IAAI,CAACkB,OAAO,GAAGjB,mBAAmBD,eAAeD;YACnD,CAAC;QACH;IACF;AACF,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { type ErrorMessages } from '../types.js';
2
+ /**
3
+ * A function that generates an error message based on provided error code and message parameters. It is used only internally.
4
+ *
5
+ * @param errorCode Error code.
6
+ * @param errorMessages Object containing all possible error messages.
7
+ * @param messageParameters Array of values that are used as message parameters.
8
+ * @return Error message.
9
+ */
10
+ export declare function createErrorMessage<T extends string>(errorCode: T, errorMessages: ErrorMessages<T>, messageParameters?: unknown[]): string;
@@ -0,0 +1,17 @@
1
+ import { ERROR_MESSAGE_TOKEN_PATTERN } from '../constants.js';
2
+ /**
3
+ * A function that generates an error message based on provided error code and message parameters. It is used only internally.
4
+ *
5
+ * @param errorCode Error code.
6
+ * @param errorMessages Object containing all possible error messages.
7
+ * @param messageParameters Array of values that are used as message parameters.
8
+ * @return Error message.
9
+ */ export function createErrorMessage(errorCode, errorMessages, messageParameters = []) {
10
+ let errorMessage = errorMessages[errorCode];
11
+ if (!errorMessage) {
12
+ return '';
13
+ }
14
+ return `${errorCode}: ${errorMessage.replace(ERROR_MESSAGE_TOKEN_PATTERN, (match, index)=>String(messageParameters[index]))}`;
15
+ }
16
+
17
+ //# sourceMappingURL=createErrorMessage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/internals/createErrorMessage.ts"],"sourcesContent":["import {ERROR_MESSAGE_TOKEN_PATTERN} from '../constants.js';\nimport {type ErrorMessages} from '../types.js';\n\n/**\n * A function that generates an error message based on provided error code and message parameters. It is used only internally.\n *\n * @param errorCode Error code.\n * @param errorMessages Object containing all possible error messages.\n * @param messageParameters Array of values that are used as message parameters.\n * @return Error message.\n */\nexport function createErrorMessage<T extends string>(\n errorCode: T,\n errorMessages: ErrorMessages<T>,\n messageParameters: unknown[] = [],\n): string {\n let errorMessage = errorMessages[errorCode];\n\n if (!errorMessage) {\n return '';\n }\n\n return `${errorCode}: ${errorMessage.replace(\n ERROR_MESSAGE_TOKEN_PATTERN,\n (match: string, index: number) => String(messageParameters[index]),\n )}`;\n}\n"],"names":["ERROR_MESSAGE_TOKEN_PATTERN","createErrorMessage","errorCode","errorMessages","messageParameters","errorMessage","replace","match","index","String"],"mappings":"AAAA,SAAQA,2BAA2B,QAAO,kBAAkB;AAG5D;;;;;;;CAOC,GACD,OAAO,SAASC,mBACdC,SAAY,EACZC,aAA+B,EAC/BC,oBAA+B,EAAE,EACzB;IACR,IAAIC,eAAeF,aAAa,CAACD,UAAU;IAE3C,IAAI,CAACG,cAAc;QACjB,OAAO;IACT,CAAC;IAED,OAAO,CAAC,EAAEH,UAAU,EAAE,EAAEG,aAAaC,OAAO,CAC1CN,6BACA,CAACO,OAAeC,QAAkBC,OAAOL,iBAAiB,CAACI,MAAM,GACjE,CAAC;AACL,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './internals/createErrorMessage.js';
@@ -0,0 +1,3 @@
1
+ export * from './internals/createErrorMessage.js';
2
+
3
+ //# sourceMappingURL=internals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/internals.ts"],"sourcesContent":["export * from './internals/createErrorMessage.js';\n"],"names":[],"mappings":"AAAA,cAAc,oCAAoC"}
package/dist/main.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './createCustomError.js';
2
+ export * from './createCustomErrorWithData.js';
2
3
  export * from './types.js';
4
+ export { UNKNOWN_ERROR } from './constants.js';
package/dist/main.js CHANGED
@@ -1,4 +1,6 @@
1
- export * from "./createCustomError.js";
2
- export * from "./types.js";
1
+ export * from './createCustomError.js';
2
+ export * from './createCustomErrorWithData.js';
3
+ export * from './types.js';
4
+ export { UNKNOWN_ERROR } from './constants.js';
3
5
 
4
6
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './createCustomError.js';\nexport * from './types.js';\n"],"names":[],"mappings":"AAAA,cAAc,yBAAyB;AACvC,cAAc,aAAa"}
1
+ {"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './createCustomError.js';\nexport * from './createCustomErrorWithData.js';\nexport * from './types.js';\nexport {UNKNOWN_ERROR} from './constants.js';\n"],"names":["UNKNOWN_ERROR"],"mappings":"AAAA,cAAc,yBAAyB;AACvC,cAAc,iCAAiC;AAC/C,cAAc,aAAa;AAC3B,SAAQA,aAAa,QAAO,iBAAiB"}
@@ -1,10 +1,8 @@
1
- import type { UNKNOWN_ERROR } from '../constants.js';
1
+ import { type UNKNOWN_ERROR } from '../constants.js';
2
2
  /**
3
3
  * Type representing custom error.
4
4
  */
5
- export declare type CustomError<ErrorCode extends string, ErrorData> = Error & {
5
+ export type CustomError<ErrorCode extends string> = Error & {
6
6
  /** String that is used to quickly determine the nature of the error and why it could have occurred. */
7
7
  code: ErrorCode | typeof UNKNOWN_ERROR;
8
- /** Data related to the error that can be used during the error handling process to gain more information or take an action. */
9
- data?: ErrorData;
10
8
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/types/CustomError.ts"],"sourcesContent":["import type {UNKNOWN_ERROR} from '../constants.js';\n\n/**\n * Type representing custom error.\n */\nexport type CustomError<ErrorCode extends string, ErrorData> = Error & {\n /** String that is used to quickly determine the nature of the error and why it could have occurred. */\n code: ErrorCode | typeof UNKNOWN_ERROR;\n\n /** Data related to the error that can be used during the error handling process to gain more information or take an action. */\n data?: ErrorData;\n};\n"],"names":[],"mappings":"AAAA,WAWE"}
1
+ {"version":3,"sources":["../../src/types/CustomError.ts"],"sourcesContent":["import {type UNKNOWN_ERROR} from '../constants.js';\n\n/**\n * Type representing custom error.\n */\nexport type CustomError<ErrorCode extends string> = Error & {\n /** String that is used to quickly determine the nature of the error and why it could have occurred. */\n code: ErrorCode | typeof UNKNOWN_ERROR;\n};\n"],"names":[],"mappings":"AAAA,WAQE"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Type representing options object used when instantianting class returned from {@linkcode createCustomError}.
3
+ */
4
+ export type CustomErrorOptions = {
5
+ /** Array of values that are used as parameters to generate the error message. */
6
+ messageParameters?: unknown[];
7
+ /** Used for setting `cause` property of an error instance that indicates the specific original cause of the error. */
8
+ cause?: unknown;
9
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Type representing options object used when instantianting class returned from {@linkcode createCustomError}.
3
+ */ export { };
4
+
5
+ //# sourceMappingURL=CustomErrorOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/types/CustomErrorOptions.ts"],"sourcesContent":["/**\n * Type representing options object used when instantianting class returned from {@linkcode createCustomError}.\n */\nexport type CustomErrorOptions = {\n /** Array of values that are used as parameters to generate the error message. */\n messageParameters?: unknown[];\n\n /** Used for setting `cause` property of an error instance that indicates the specific original cause of the error. */\n cause?: unknown;\n};\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAME"}
@@ -0,0 +1,10 @@
1
+ import type { UNKNOWN_ERROR } from '../constants.js';
2
+ /**
3
+ * Type representing custom error that can have an arbitrary data attached.
4
+ */
5
+ export type CustomErrorWithData<ErrorCode extends string, ErrorData> = Error & {
6
+ /** String that is used to quickly determine the nature of the error and why it could have occurred. */
7
+ code: ErrorCode | typeof UNKNOWN_ERROR;
8
+ /** Data related to the error that can be used during the error handling process to gain more information or take an action. */
9
+ data?: ErrorData;
10
+ };
@@ -0,0 +1,3 @@
1
+ export { };
2
+
3
+ //# sourceMappingURL=CustomErrorWithData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/types/CustomErrorWithData.ts"],"sourcesContent":["import type {UNKNOWN_ERROR} from '../constants.js';\n\n/**\n * Type representing custom error that can have an arbitrary data attached.\n */\nexport type CustomErrorWithData<ErrorCode extends string, ErrorData> = Error & {\n /** String that is used to quickly determine the nature of the error and why it could have occurred. */\n code: ErrorCode | typeof UNKNOWN_ERROR;\n\n /** Data related to the error that can be used during the error handling process to gain more information or take an action. */\n data?: ErrorData;\n};\n"],"names":[],"mappings":"AAAA,WAWE"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Type representing options object used when instantianting class returned from {@linkcode createCustomErrorWithData}.
3
+ */
4
+ export type CustomErrorWithDataOptions<ErrorData> = {
5
+ /** Array of values that are used as parameters to generate the error message. */
6
+ messageParameters?: unknown[];
7
+ /** Used for setting `cause` property of an error instance that indicates the specific original cause of the error. */
8
+ cause?: unknown;
9
+ /** Custom data that will be attached to the error instance. */
10
+ data?: ErrorData;
11
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Type representing options object used when instantianting class returned from {@linkcode createCustomErrorWithData}.
3
+ */ export { };
4
+
5
+ //# sourceMappingURL=CustomErrorWithDataOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/types/CustomErrorWithDataOptions.ts"],"sourcesContent":["/**\n * Type representing options object used when instantianting class returned from {@linkcode createCustomErrorWithData}.\n */\nexport type CustomErrorWithDataOptions<ErrorData> = {\n /** Array of values that are used as parameters to generate the error message. */\n messageParameters?: unknown[];\n\n /** Used for setting `cause` property of an error instance that indicates the specific original cause of the error. */\n cause?: unknown;\n\n /** Custom data that will be attached to the error instance. */\n data?: ErrorData;\n};\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WASE"}
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Object type whose property keys are `ErrorCode` and whose property values are `string`.
3
3
  */
4
- export declare type ErrorMessages<ErrorCode extends string> = Record<ErrorCode, string>;
4
+ export type ErrorMessages<ErrorCode extends string> = Record<ErrorCode, string>;
package/dist/types.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  export * from './types/CustomError.js';
2
+ export * from './types/CustomErrorOptions.js';
3
+ export * from './types/CustomErrorWithData.js';
4
+ export * from './types/CustomErrorWithDataOptions.js';
2
5
  export * from './types/ErrorMessages.js';
package/dist/types.js CHANGED
@@ -1,4 +1,7 @@
1
- export * from "./types/CustomError.js";
2
- export * from "./types/ErrorMessages.js";
1
+ export * from './types/CustomError.js';
2
+ export * from './types/CustomErrorOptions.js';
3
+ export * from './types/CustomErrorWithData.js';
4
+ export * from './types/CustomErrorWithDataOptions.js';
5
+ export * from './types/ErrorMessages.js';
3
6
 
4
7
  //# sourceMappingURL=types.js.map
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts"],"sourcesContent":["export * from './types/CustomError.js';\nexport * from './types/ErrorMessages.js';\n"],"names":[],"mappings":"AAAA,cAAc,yBAAyB;AACvC,cAAc,2BAA2B"}
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["export * from './types/CustomError.js';\nexport * from './types/CustomErrorOptions.js';\nexport * from './types/CustomErrorWithData.js';\nexport * from './types/CustomErrorWithDataOptions.js';\nexport * from './types/ErrorMessages.js';\n"],"names":[],"mappings":"AAAA,cAAc,yBAAyB;AACvC,cAAc,gCAAgC;AAC9C,cAAc,iCAAiC;AAC/C,cAAc,wCAAwC;AACtD,cAAc,2BAA2B"}
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@jakubmazanec/error",
3
- "version": "1.0.1-next.8fca4dc",
3
+ "version": "2.0.0-9cfd7b2",
4
4
  "description": "Collection of utilities for creating and handling errors.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git@github.com:jakubmazanec/js-tools.git",
8
8
  "directory": "packages/error"
9
9
  },
10
- "license": "MIT",
10
+ "license": "LGPL-3.0-only",
11
11
  "author": "Jakub Mazanec <jakub@mazanec.dev>",
12
12
  "sideEffects": false,
13
13
  "type": "module",
@@ -22,14 +22,17 @@
22
22
  "document": "typedoc --options typedoc.config.cjs",
23
23
  "format": "prettier . --write --ignore-unknown --ignore-path ../../.prettierignore",
24
24
  "lint": "eslint .",
25
- "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
25
+ "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests",
26
26
  "typecheck": "tsc --project tsconfig.typecheck.json"
27
27
  },
28
+ "dependencies": {
29
+ "zod": "^3.21.4"
30
+ },
28
31
  "devDependencies": {
29
- "@jest/globals": "^28.1.3"
32
+ "@jest/globals": "^29.5.0"
30
33
  },
31
- "packageManager": "npm@8.19.2",
34
+ "packageManager": "npm@8.19.4",
32
35
  "engines": {
33
- "node": ">=16.18.1"
36
+ "node": ">=16.20.0"
34
37
  }
35
38
  }
package/src/constants.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  export const ERROR_MESSAGE_TOKEN_PATTERN = /\{(\d+)\}/gu;
2
2
 
3
+ /**
4
+ * Code for an unknown error. You should never use nor encounter this error code.
5
+ */
3
6
  export const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
4
7
 
5
8
  export const INTERNAL_ERROR_MESSAGES = {
@@ -1,62 +1,37 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any -- needed */
2
- import {ERROR_MESSAGE_TOKEN_PATTERN, INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR} from './constants.js';
3
- import type {CustomError, ErrorMessages} from './types.js';
4
-
5
- function createErrorMessage<T extends string>(
6
- errorCode: T,
7
- errorMessages: ErrorMessages<T>,
8
- messageParameters: unknown[] = [],
9
- ): string {
10
- let errorMessage = errorMessages[errorCode];
11
-
12
- if (!errorMessage) {
13
- return '';
14
- }
15
-
16
- return `${errorCode}: ${errorMessage.replace(
17
- ERROR_MESSAGE_TOKEN_PATTERN,
18
- (match: string, index: number) => String(messageParameters[index]),
19
- )}`;
20
- }
21
-
22
- export interface CustomErrorOptions<T> {
23
- messageParameters?: unknown[];
24
- data: T;
25
- }
1
+ import {INTERNAL_ERROR_MESSAGES, UNKNOWN_ERROR} from './constants.js';
2
+ import {createErrorMessage} from './internals.js';
3
+ import {type CustomError, type CustomErrorOptions, type ErrorMessages} from './types.js';
26
4
 
27
5
  /**
28
- * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error`, simplifies
29
- * generating error messages via predefined error codes and allows attaching custom data to the error instance.
6
+ * Function for creating custom error classes. Such custom error class properly subclasses built-in `Error` and simplifies
7
+ * generating error messages via predefined error codes.
8
+ *
9
+ * @param errorName Error name.
10
+ * @param errorMessages Object containing all possible error messages.
11
+ * @return Class that implements {@linkcode CustomError}.
30
12
  */
31
- export function createCustomError<
32
- ErrorCode extends string,
33
- // eslint-disable-next-line @typescript-eslint/no-redeclare -- because `ErrorData` is used only as a type, we don't mind
34
- ErrorData extends abstract new (...args: any) => any,
35
- >(
13
+ export function createCustomError<ErrorCode extends string>(
36
14
  errorName: string,
37
15
  errorMessages: ErrorMessages<ErrorCode>,
38
- ErrorData?: ErrorData,
39
- ): new (code: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) => CustomError<
40
- ErrorCode | typeof UNKNOWN_ERROR,
41
- InstanceType<ErrorData>
16
+ ): new (code: ErrorCode, options?: CustomErrorOptions) => CustomError<
17
+ ErrorCode | typeof UNKNOWN_ERROR
42
18
  > {
43
- return class InternalCustomError
44
- extends Error
45
- implements CustomError<ErrorCode, InstanceType<ErrorData>>
46
- {
19
+ return class InternalCustomError extends Error implements CustomError<ErrorCode> {
47
20
  code: ErrorCode | typeof UNKNOWN_ERROR;
48
- data?: InstanceType<ErrorData>;
49
21
 
50
- constructor(errorCode: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) {
51
- super(createErrorMessage(errorCode, errorMessages, options?.messageParameters));
22
+ constructor(errorCode: ErrorCode, options?: CustomErrorOptions) {
23
+ let errorMessage = createErrorMessage(errorCode, errorMessages, options?.messageParameters);
24
+ let errorOptions: ErrorOptions = {};
25
+
26
+ if (typeof options?.cause !== 'undefined') {
27
+ errorOptions.cause = options.cause;
28
+ }
29
+
30
+ super(errorMessage, errorOptions);
52
31
 
53
32
  this.code = errorCode;
54
33
  this.name = errorName;
55
34
 
56
- if (typeof options?.data !== 'undefined') {
57
- this.data = options.data;
58
- }
59
-
60
35
  // if the message is empty, we are making this an unknown error
61
36
  if (!this.message) {
62
37
  this.code = UNKNOWN_ERROR;