@jakubmazanec/error 3.0.18 → 3.0.19-unstable.72f063e3
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/build/createCustomError.d.ts +2 -2
- package/build/createCustomError.js.map +1 -1
- package/build/createCustomErrorWithData.d.ts +3 -2
- package/build/createCustomErrorWithData.js.map +2 -2
- package/build/internals/createErrorMessage.d.ts +2 -1
- package/build/internals/createErrorMessage.js.map +2 -2
- package/build/types/CustomError.d.ts +4 -1
- package/build/types/CustomErrorOptions.d.ts +6 -2
- package/build/types/CustomErrorWithData.d.ts +8 -2
- package/build/types/CustomErrorWithDataOptions.d.ts +6 -2
- package/package.json +2 -2
- package/source/createCustomError.ts +2 -2
- package/source/createCustomErrorWithData.ts +3 -2
- package/source/internals/createErrorMessage.ts +2 -1
- package/source/types/CustomError.ts +4 -1
- package/source/types/CustomErrorOptions.ts +6 -2
- package/source/types/CustomErrorWithData.ts +8 -2
- package/source/types/CustomErrorWithDataOptions.ts +6 -2
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { UNKNOWN_ERROR } from './constants.js';
|
|
2
2
|
import { type CustomError, type CustomErrorOptions, type ErrorMessages } from './types.js';
|
|
3
3
|
/**
|
|
4
|
-
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
5
|
-
* generating error messages via predefined error codes.
|
|
4
|
+
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
5
|
+
* `Error` and simplifies generating error messages via predefined error codes.
|
|
6
6
|
*
|
|
7
7
|
* @param errorName Error name.
|
|
8
8
|
* @param errorMessages Object containing all possible error messages.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../source/createCustomError.ts"],
|
|
4
|
-
"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
|
|
4
|
+
"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\n * `Error` and simplifies 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 {@link CustomError}.\n */\nexport function createCustomError<ErrorCode extends string>(\n errorName: string,\n errorMessages: ErrorMessages<ErrorCode>,\n): new (\n code: ErrorCode,\n options?: CustomErrorOptions,\n) => CustomError<ErrorCode | typeof UNKNOWN_ERROR> {\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 (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"],
|
|
5
5
|
"mappings": "AAAA,SAAQ,yBAAyB,qBAAoB;AACrD,SAAQ,0BAAyB;AAW1B,gBAAS,kBACd,WACA,eAIiD;AACjD,SAAO,MAAM,4BAA4B,MAAwC;AAAA,IAC/E;AAAA,IAEA,YAAY,WAAsB,SAA8B;AAC9D,UAAI,eAAe,mBAAmB,WAAW,eAAe,SAAS,iBAAiB;AAC1F,UAAI,eAA6B,CAAC;AAElC,UAAI,SAAS,UAAU,QAAW;AAChC,qBAAa,QAAQ,QAAQ;AAAA,MAC/B;AAEA,YAAM,cAAc,YAAY;AAEhC,WAAK,OAAO;AACZ,WAAK,OAAO;AAGZ,UAAI,CAAC,KAAK,SAAS;AACjB,aAAK,OAAO;AACZ,aAAK,UAAU,mBAAmB,eAAe,uBAAuB;AAAA,MAC1E;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -2,8 +2,9 @@ import { type z, type ZodObject } from 'zod';
|
|
|
2
2
|
import { UNKNOWN_ERROR } from './constants.js';
|
|
3
3
|
import { type CustomErrorWithData, type CustomErrorWithDataOptions, type ErrorMessages } from './types.js';
|
|
4
4
|
/**
|
|
5
|
-
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
6
|
-
* generating error messages via predefined error codes and allows attaching
|
|
5
|
+
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
6
|
+
* `Error`, simplifies generating error messages via predefined error codes and allows attaching
|
|
7
|
+
* custom data to the error instance.
|
|
7
8
|
*
|
|
8
9
|
* @param errorName Error name.
|
|
9
10
|
* @param errorMessages Object containing all possible error messages.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../source/createCustomErrorWithData.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type z, type ZodObject} 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
|
|
5
|
-
"mappings": "AAGA,SAAQ,yBAAyB,qBAAoB;AACrD,SAAQ,0BAAyB;
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any -- needed */\nimport {type z, type ZodObject} 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\n * `Error`, simplifies generating error messages via predefined error codes and allows attaching\n * 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 {@link CustomErrorWithData}.\n */\nexport function createCustomErrorWithData<ErrorCode extends string, ErrorData extends ZodObject>(\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 (\n code: ErrorCode,\n options?: CustomErrorWithDataOptions<ErrorData>,\n) => CustomErrorWithData<ErrorCode | typeof UNKNOWN_ERROR, ErrorData> {\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 (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 (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"],
|
|
5
|
+
"mappings": "AAGA,SAAQ,yBAAyB,qBAAoB;AACrD,SAAQ,0BAAyB;AAoC1B,gBAAS,0BACd,WACA,eACA,WAIoE;AACpE,SAAO,MAAM,4BACH,MAEV;AAAA,IACE;AAAA,IACA;AAAA,IAEA,YAAY,WAAsB,SAAiD;AACjF,UAAI,eAAe,mBAAmB,WAAW,eAAe,SAAS,iBAAiB;AAC1F,UAAI,eAA6B,CAAC;AAElC,UAAI,SAAS,UAAU,QAAW;AAChC,qBAAa,QAAQ,QAAQ;AAAA,MAC/B;AAEA,YAAM,cAAc,YAAY;AAEhC,WAAK,OAAO;AACZ,WAAK,OAAO;AAEZ,UAAI,SAAS,SAAS,QAAW;AAC/B,aAAK,OAAO,QAAQ;AAAA,MACtB;AAGA,UAAI,CAAC,KAAK,SAAS;AACjB,aAAK,OAAO;AACZ,aAAK,UAAU,mBAAmB,eAAe,uBAAuB;AAAA,MAC1E;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ErrorMessages } from '../types.js';
|
|
2
2
|
/**
|
|
3
|
-
* A function that generates an error message based on provided error code and message parameters.
|
|
3
|
+
* A function that generates an error message based on provided error code and message parameters.
|
|
4
|
+
* It is used only internally.
|
|
4
5
|
*
|
|
5
6
|
* @param errorCode Error code.
|
|
6
7
|
* @param errorMessages Object containing all possible error messages.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../source/internals/createErrorMessage.ts"],
|
|
4
|
-
"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
|
|
5
|
-
"mappings": "AAAA,SAAQ,mCAAkC;
|
|
4
|
+
"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.\n * 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"],
|
|
5
|
+
"mappings": "AAAA,SAAQ,mCAAkC;AAYnC,gBAAS,mBACd,WACA,eACA,oBAA+B,CAAC,GACxB;AACR,MAAI,eAAe,cAAc,SAAS;AAE1C,MAAI,CAAC,cAAc;AACjB,WAAO;AAAA,EACT;AAEA,SAAO,GAAG,SAAS,KAAK,aAAa;AAAA,IACnC;AAAA,IACA,CAAC,OAAe,UAAkB,OAAO,kBAAkB,KAAK,CAAC;AAAA,EACnE,CAAC;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -3,6 +3,9 @@ import { type UNKNOWN_ERROR } from '../constants.js';
|
|
|
3
3
|
* Type representing custom error.
|
|
4
4
|
*/
|
|
5
5
|
export type CustomError<ErrorCode extends string> = Error & {
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* String that is used to quickly determine the nature of the error and why it could have
|
|
8
|
+
* occurred.
|
|
9
|
+
*/
|
|
7
10
|
code: ErrorCode | typeof UNKNOWN_ERROR;
|
|
8
11
|
};
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Type representing options object used when instantianting class returned from {@link
|
|
2
|
+
* Type representing options object used when instantianting class returned from {@link
|
|
3
|
+
* createCustomError}.
|
|
3
4
|
*/
|
|
4
5
|
export type CustomErrorOptions = {
|
|
5
6
|
/** Array of values that are used as parameters to generate the error message. */
|
|
6
7
|
messageParameters?: unknown[];
|
|
7
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Used for setting `cause` property of an error instance that indicates the specific original
|
|
10
|
+
* cause of the error.
|
|
11
|
+
*/
|
|
8
12
|
cause?: unknown;
|
|
9
13
|
};
|
|
@@ -3,8 +3,14 @@ import { type UNKNOWN_ERROR } from '../constants.js';
|
|
|
3
3
|
* Type representing custom error that can have an arbitrary data attached.
|
|
4
4
|
*/
|
|
5
5
|
export type CustomErrorWithData<ErrorCode extends string, ErrorData> = Error & {
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* String that is used to quickly determine the nature of the error and why it could have
|
|
8
|
+
* occurred.
|
|
9
|
+
*/
|
|
7
10
|
code: ErrorCode | typeof UNKNOWN_ERROR;
|
|
8
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Data related to the error that can be used during the error handling process to gain more
|
|
13
|
+
* information or take an action.
|
|
14
|
+
*/
|
|
9
15
|
data?: ErrorData;
|
|
10
16
|
};
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Type representing options object used when instantianting class returned from {@link
|
|
2
|
+
* Type representing options object used when instantianting class returned from {@link
|
|
3
|
+
* createCustomErrorWithData}.
|
|
3
4
|
*/
|
|
4
5
|
export type CustomErrorWithDataOptions<ErrorData> = {
|
|
5
6
|
/** Array of values that are used as parameters to generate the error message. */
|
|
6
7
|
messageParameters?: unknown[];
|
|
7
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Used for setting `cause` property of an error instance that indicates the specific original
|
|
10
|
+
* cause of the error.
|
|
11
|
+
*/
|
|
8
12
|
cause?: unknown;
|
|
9
13
|
/** Custom data that will be attached to the error instance. */
|
|
10
14
|
data?: ErrorData;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jakubmazanec/error",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.19-unstable.72f063e3",
|
|
4
4
|
"description": "Collection of utilities for creating and handling errors.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"zod": "^4.4.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@jakubmazanec/eslint-config": "
|
|
33
|
+
"@jakubmazanec/eslint-config": "6.0.0-unstable.72f063e3",
|
|
34
34
|
"@types/lodash": "^4.17.24",
|
|
35
35
|
"@types/node": "^24.0.4",
|
|
36
36
|
"@vitest/coverage-v8": "^4.1.10",
|
|
@@ -3,8 +3,8 @@ import {createErrorMessage} from './internals.js';
|
|
|
3
3
|
import {type CustomError, type CustomErrorOptions, type ErrorMessages} from './types.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
7
|
-
* generating error messages via predefined error codes.
|
|
6
|
+
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
7
|
+
* `Error` and simplifies generating error messages via predefined error codes.
|
|
8
8
|
*
|
|
9
9
|
* @param errorName Error name.
|
|
10
10
|
* @param errorMessages Object containing all possible error messages.
|
|
@@ -10,8 +10,9 @@ import {
|
|
|
10
10
|
} from './types.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
14
|
-
* generating error messages via predefined error codes and allows attaching
|
|
13
|
+
* Function for creating custom error classes. Such custom error class properly subclasses built-in
|
|
14
|
+
* `Error`, simplifies generating error messages via predefined error codes and allows attaching
|
|
15
|
+
* custom data to the error instance.
|
|
15
16
|
*
|
|
16
17
|
* @param errorName Error name.
|
|
17
18
|
* @param errorMessages Object containing all possible error messages.
|
|
@@ -2,7 +2,8 @@ import {ERROR_MESSAGE_TOKEN_PATTERN} from '../constants.js';
|
|
|
2
2
|
import {type ErrorMessages} from '../types.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* A function that generates an error message based on provided error code and message parameters.
|
|
5
|
+
* A function that generates an error message based on provided error code and message parameters.
|
|
6
|
+
* It is used only internally.
|
|
6
7
|
*
|
|
7
8
|
* @param errorCode Error code.
|
|
8
9
|
* @param errorMessages Object containing all possible error messages.
|
|
@@ -4,6 +4,9 @@ import {type UNKNOWN_ERROR} from '../constants.js';
|
|
|
4
4
|
* Type representing custom error.
|
|
5
5
|
*/
|
|
6
6
|
export type CustomError<ErrorCode extends string> = Error & {
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* String that is used to quickly determine the nature of the error and why it could have
|
|
9
|
+
* occurred.
|
|
10
|
+
*/
|
|
8
11
|
code: ErrorCode | typeof UNKNOWN_ERROR;
|
|
9
12
|
};
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Type representing options object used when instantianting class returned from {@link
|
|
2
|
+
* Type representing options object used when instantianting class returned from {@link
|
|
3
|
+
* createCustomError}.
|
|
3
4
|
*/
|
|
4
5
|
export type CustomErrorOptions = {
|
|
5
6
|
/** Array of values that are used as parameters to generate the error message. */
|
|
6
7
|
messageParameters?: unknown[];
|
|
7
8
|
|
|
8
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Used for setting `cause` property of an error instance that indicates the specific original
|
|
11
|
+
* cause of the error.
|
|
12
|
+
*/
|
|
9
13
|
cause?: unknown;
|
|
10
14
|
};
|
|
@@ -4,9 +4,15 @@ import {type UNKNOWN_ERROR} from '../constants.js';
|
|
|
4
4
|
* Type representing custom error that can have an arbitrary data attached.
|
|
5
5
|
*/
|
|
6
6
|
export type CustomErrorWithData<ErrorCode extends string, ErrorData> = Error & {
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* String that is used to quickly determine the nature of the error and why it could have
|
|
9
|
+
* occurred.
|
|
10
|
+
*/
|
|
8
11
|
code: ErrorCode | typeof UNKNOWN_ERROR;
|
|
9
12
|
|
|
10
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Data related to the error that can be used during the error handling process to gain more
|
|
15
|
+
* information or take an action.
|
|
16
|
+
*/
|
|
11
17
|
data?: ErrorData;
|
|
12
18
|
};
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Type representing options object used when instantianting class returned from {@link
|
|
2
|
+
* Type representing options object used when instantianting class returned from {@link
|
|
3
|
+
* createCustomErrorWithData}.
|
|
3
4
|
*/
|
|
4
5
|
export type CustomErrorWithDataOptions<ErrorData> = {
|
|
5
6
|
/** Array of values that are used as parameters to generate the error message. */
|
|
6
7
|
messageParameters?: unknown[];
|
|
7
8
|
|
|
8
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Used for setting `cause` property of an error instance that indicates the specific original
|
|
11
|
+
* cause of the error.
|
|
12
|
+
*/
|
|
9
13
|
cause?: unknown;
|
|
10
14
|
|
|
11
15
|
/** Custom data that will be attached to the error instance. */
|