@jakubmazanec/error 1.0.0-1fd57ce
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/LICENSE +18 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +7 -0
- package/dist/constants.js.map +1 -0
- package/dist/createCustomError.d.ts +11 -0
- package/dist/createCustomError.js +30 -0
- package/dist/createCustomError.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +4 -0
- package/dist/main.js.map +1 -0
- package/dist/types/CustomError.d.ts +10 -0
- package/dist/types/CustomError.js +3 -0
- package/dist/types/CustomError.js.map +1 -0
- package/dist/types/ErrorMessages.d.ts +4 -0
- package/dist/types/ErrorMessages.js +5 -0
- package/dist/types/ErrorMessages.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/package.json +35 -0
- package/src/constants.ts +7 -0
- package/src/createCustomError.ts +67 -0
- package/src/main.ts +2 -0
- package/src/types/CustomError.ts +12 -0
- package/src/types/ErrorMessages.ts +4 -0
- package/src/types.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Jakub Mazanec
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
6
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
7
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
8
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
12
|
+
portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
15
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
16
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
17
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +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,2BAA2B,gBAAgB,CAAC;AAEzD,OAAO,MAAMC,aAAa,GAAG,eAAe,CAAC;AAE7C,OAAO,MAAMC,uBAAuB,GAAG;IACrC,CAACD,aAAa,CAAC,EAAE,gCAAgC;CAClD,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
7
|
+
/**
|
|
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.
|
|
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>>;
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
}
|
|
9
|
+
/**
|
|
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) {
|
|
13
|
+
return class InternalCustomError extends Error {
|
|
14
|
+
constructor(errorCode, options){
|
|
15
|
+
super(createErrorMessage(errorCode, errorMessages, options === null || options === void 0 ? void 0 : options.messageParameters));
|
|
16
|
+
this.code = errorCode;
|
|
17
|
+
this.name = errorName;
|
|
18
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.data) !== "undefined") {
|
|
19
|
+
this.data = options.data;
|
|
20
|
+
}
|
|
21
|
+
// if the message is empty, we are making this an unknown error
|
|
22
|
+
if (!this.message) {
|
|
23
|
+
this.code = UNKNOWN_ERROR;
|
|
24
|
+
this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=createCustomError.js.map
|
|
@@ -0,0 +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,iEAAiE,CACjE,SAAQA,2BAA2B,EAAEC,uBAAuB,EAAEC,aAAa,QAAO,gBAAgB,CAAC;AAGnG,SAASC,kBAAkB,CACzBC,SAAY,EACZC,aAA+B,EAC/BC,iBAA4B,GAAG,EAAE,EACzB;IACR,IAAIC,YAAY,GAAGF,aAAa,CAACD,SAAS,CAAC,AAAC;IAE5C,IAAI,CAACG,YAAY,EAAE;QACjB,OAAO,EAAE,CAAC;KACX;IAED,OAAO,CAAC,EAAEH,SAAS,CAAC,EAAE,EAAEG,YAAY,CAACC,OAAO,CAC1CR,2BAA2B,EAC3B,CAACS,KAAa,EAAEC,KAAa,GAAKC,MAAM,CAACL,iBAAiB,CAACI,KAAK,CAAC,CAAC,CACnE,CAAC,CAAC,CAAC;CACL;AAOD;;;GAGG,CACH,OAAO,SAASE,iBAAiB,CAK/BC,SAAiB,EACjBR,aAAuC,EACvCS,SAAqB,EAIrB;IACA,OAAO,MAAMC,mBAAmB,SACtBC,KAAK;QAMbC,YAAYb,SAAoB,EAAEc,OAAqD,CAAE;YACvF,KAAK,CAACf,kBAAkB,CAACC,SAAS,EAAEC,aAAa,EAAEa,OAAO,aAAPA,OAAO,WAAmB,GAA1BA,KAAAA,CAA0B,GAA1BA,OAAO,CAAEZ,iBAAiB,CAAC,CAAC,CAAC;YAEhF,IAAI,CAACa,IAAI,GAAGf,SAAS,CAAC;YACtB,IAAI,CAACgB,IAAI,GAAGP,SAAS,CAAC;YAEtB,IAAI,OAAOK,CAAAA,OAAO,aAAPA,OAAO,WAAM,GAAbA,KAAAA,CAAa,GAAbA,OAAO,CAAEG,IAAI,CAAA,KAAK,WAAW,EAAE;gBACxC,IAAI,CAACA,IAAI,GAAGH,OAAO,CAACG,IAAI,CAAC;aAC1B;YAED,+DAA+D;YAC/D,IAAI,CAAC,IAAI,CAACC,OAAO,EAAE;gBACjB,IAAI,CAACH,IAAI,GAAGjB,aAAa,CAAC;gBAC1B,IAAI,CAACoB,OAAO,GAAGnB,kBAAkB,CAACD,aAAa,EAAED,uBAAuB,CAAC,CAAC;aAC3E;SACF;KACF,CAAC;CACH"}
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './createCustomError.js';\nexport * from './types.js';\n"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { UNKNOWN_ERROR } from '../constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type representing custom error.
|
|
4
|
+
*/
|
|
5
|
+
export declare type CustomError<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 @@
|
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/types/ErrorMessages.ts"],"sourcesContent":["/**\n * Object type whose property keys are `ErrorCode` and whose property values are `string`.\n */\nexport type ErrorMessages<ErrorCode extends string> = Record<ErrorCode, string>;\n"],"names":[],"mappings":"AAAA;;GAEG,CACH,WAAgF"}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["export * from './types/CustomError.js';\nexport * from './types/ErrorMessages.js';\n"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jakubmazanec/error",
|
|
3
|
+
"version": "1.0.0-1fd57ce",
|
|
4
|
+
"description": "Collection of utilities for creating and handling errors.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@github.com:jakubmazanec/js-tools.git",
|
|
8
|
+
"directory": "packages/error"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "Jakub Mazanec <jakub@mazanec.dev>",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": "./dist/main.js",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "swc src -d dist && tsc",
|
|
21
|
+
"clean": "del-cli dist *.tsbuildinfo",
|
|
22
|
+
"document": "typedoc --options typedoc.config.cjs",
|
|
23
|
+
"format": "prettier . --write --ignore-unknown --ignore-path ../../.prettierignore",
|
|
24
|
+
"lint": "eslint .",
|
|
25
|
+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
|
|
26
|
+
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@jest/globals": "^28.1.3"
|
|
30
|
+
},
|
|
31
|
+
"packageManager": "npm@8.11.0",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=16.15.0"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
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.
|
|
30
|
+
*/
|
|
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
|
+
>(
|
|
36
|
+
errorName: string,
|
|
37
|
+
errorMessages: ErrorMessages<ErrorCode>,
|
|
38
|
+
ErrorData?: ErrorData,
|
|
39
|
+
): new (code: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) => CustomError<
|
|
40
|
+
ErrorCode | typeof UNKNOWN_ERROR,
|
|
41
|
+
InstanceType<ErrorData>
|
|
42
|
+
> {
|
|
43
|
+
return class InternalCustomError
|
|
44
|
+
extends Error
|
|
45
|
+
implements CustomError<ErrorCode, InstanceType<ErrorData>>
|
|
46
|
+
{
|
|
47
|
+
code: ErrorCode | typeof UNKNOWN_ERROR;
|
|
48
|
+
data?: InstanceType<ErrorData>;
|
|
49
|
+
|
|
50
|
+
constructor(errorCode: ErrorCode, options?: CustomErrorOptions<InstanceType<ErrorData>>) {
|
|
51
|
+
super(createErrorMessage(errorCode, errorMessages, options?.messageParameters));
|
|
52
|
+
|
|
53
|
+
this.code = errorCode;
|
|
54
|
+
this.name = errorName;
|
|
55
|
+
|
|
56
|
+
if (typeof options?.data !== 'undefined') {
|
|
57
|
+
this.data = options.data;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// if the message is empty, we are making this an unknown error
|
|
61
|
+
if (!this.message) {
|
|
62
|
+
this.code = UNKNOWN_ERROR;
|
|
63
|
+
this.message = createErrorMessage(UNKNOWN_ERROR, INTERNAL_ERROR_MESSAGES);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type {UNKNOWN_ERROR} from '../constants.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type representing custom error.
|
|
5
|
+
*/
|
|
6
|
+
export type CustomError<ErrorCode extends string, ErrorData> = Error & {
|
|
7
|
+
/** String that is used to quickly determine the nature of the error and why it could have occurred. */
|
|
8
|
+
code: ErrorCode | typeof UNKNOWN_ERROR;
|
|
9
|
+
|
|
10
|
+
/** Data related to the error that can be used during the error handling process to gain more information or take an action. */
|
|
11
|
+
data?: ErrorData;
|
|
12
|
+
};
|
package/src/types.ts
ADDED