error-message-utils 1.2.6 → 1.2.8
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/README.md +8 -1
- package/dist/error-handler/index.d.ts +19 -13
- package/dist/error-handler/index.js +1 -1
- package/dist/error-handler/utilities.d.ts +8 -0
- package/dist/error-handler/utilities.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/utils/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -96,7 +96,7 @@ z.object({
|
|
|
96
96
|
Identify encoded errors:
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
|
-
import { isEncodedError, encodeError } from 'error-message-utils';
|
|
99
|
+
import { isEncodedError, encodeError, hasErrorCode } from 'error-message-utils';
|
|
100
100
|
|
|
101
101
|
isEncodedError('Some random unencoded error');
|
|
102
102
|
// false
|
|
@@ -109,6 +109,13 @@ isEncodedError(encodeError('Some unknown error.', 'NASTY_ERROR'));
|
|
|
109
109
|
|
|
110
110
|
isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
|
|
111
111
|
// true
|
|
112
|
+
|
|
113
|
+
hasErrorCode(new Error('Oops, something went wrong.'), 'MY_ERROR_CODE'); // false
|
|
114
|
+
hasErrorCode(
|
|
115
|
+
new Exception('Oops, something went wrong.', 'MY_ERROR_CODE'),
|
|
116
|
+
'MY_ERROR_CODE'
|
|
117
|
+
);
|
|
118
|
+
// true
|
|
112
119
|
```
|
|
113
120
|
|
|
114
121
|
|
|
@@ -1,45 +1,51 @@
|
|
|
1
1
|
import { IErrorCode, IDecodedError } from '../shared/types.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
3
|
* General errors
|
|
4
|
-
|
|
4
|
+
*/
|
|
5
5
|
/**
|
|
6
6
|
* Attempts to extract an error message from an error that could be anything. If it fails to do so,
|
|
7
7
|
* it returns the default message.
|
|
8
8
|
* @param error The error to extract the message from.
|
|
9
9
|
* @returns A string containing the extracted message or the default message if extraction fails.
|
|
10
10
|
*/
|
|
11
|
-
declare const extractMessage: (error: any) => string;
|
|
12
|
-
|
|
13
|
-
* Encoding
|
|
14
|
-
|
|
11
|
+
export declare const extractMessage: (error: any) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Encoding / Decoding
|
|
14
|
+
*/
|
|
15
15
|
/**
|
|
16
16
|
* Given an error in any format, it extracts the message and inserts the code at the end.
|
|
17
17
|
* @param error The error to be encoded, can be of any type.
|
|
18
18
|
* @param code The error code to be wrapped and appended to the message.
|
|
19
19
|
* @returns A string containing the encoded error message.
|
|
20
20
|
*/
|
|
21
|
-
declare const encodeError: (error: any, code: IErrorCode) => string;
|
|
21
|
+
export declare const encodeError: (error: any, code: IErrorCode) => string;
|
|
22
22
|
/**
|
|
23
23
|
* Given an error, it will extract the encoded message and attempt to decode it. If successful,
|
|
24
24
|
* it separates the error message from the code so it can be shown directly to the user.
|
|
25
25
|
* @param error The error to be decoded, can be of any type.
|
|
26
26
|
* @returns The decoded error, containing the message and the code.
|
|
27
27
|
*/
|
|
28
|
-
declare const decodeError: (error: any) => IDecodedError;
|
|
28
|
+
export declare const decodeError: (error: any) => IDecodedError;
|
|
29
29
|
/**
|
|
30
30
|
* Determines if a given error (in any format) is an error encoded by this package.
|
|
31
31
|
* @param error The error to be checked, can be of any type.
|
|
32
32
|
* @returns A boolean indicating whether the error is an encoded error or not.
|
|
33
33
|
*/
|
|
34
|
-
declare const isEncodedError: (error: any) => boolean;
|
|
35
|
-
|
|
34
|
+
export declare const isEncodedError: (error: any) => boolean;
|
|
35
|
+
/**
|
|
36
36
|
* Misc helpers
|
|
37
|
-
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Checks if the given error matches the specified error code.
|
|
40
|
+
* @param error The error to be checked, can be of any type.
|
|
41
|
+
* @param code The error code to check against.
|
|
42
|
+
* @returns A boolean indicating whether the error matches the specified code.
|
|
43
|
+
*/
|
|
44
|
+
export declare const hasErrorCode: (error: unknown, code: IErrorCode) => boolean;
|
|
38
45
|
/**
|
|
39
46
|
* Verifies if a value matches the default error message used by this package.
|
|
40
47
|
* @param value The value to be checked.
|
|
41
48
|
* @param fullMatch Whether to check for an exact match or a partial match.
|
|
42
49
|
* @returns A boolean indicating whether the value matches the default error message.
|
|
43
50
|
*/
|
|
44
|
-
declare const isDefaultErrorMessage: (value: string, fullMatch?: boolean) => value is string;
|
|
45
|
-
export { type IErrorCode, type IDecodedError, extractMessage, encodeError, decodeError, isEncodedError, isDefaultErrorMessage, };
|
|
51
|
+
export declare const isDefaultErrorMessage: (value: string, fullMatch?: boolean) => value is string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ZodError}from"zod";import{DEFAULT_CODE,DEFAULT_MESSAGE}from"../shared/constants.js";import{wrapCode,unwrapCode}from"../utils/index.js";
|
|
1
|
+
import{ZodError}from"zod";import{DEFAULT_CODE,DEFAULT_MESSAGE}from"../shared/constants.js";import{wrapCode,unwrapCode}from"../utils/index.js";import{extractZodErrorMessage}from"./utilities.js";export const extractMessage=r=>{if("string"==typeof r&&r.length)return r;if(r instanceof ZodError)return extractZodErrorMessage(r);if(r instanceof Error&&r.message)return r.cause?`${r.message}; [CAUSE]: ${extractMessage(r.cause)}`:r.message;if(r&&"object"==typeof r){if(r.message)return extractMessage(r.message);if(r.msg)return extractMessage(r.msg);if(r.error)return extractMessage(r.error);if(r.err)return extractMessage(r.err);if(r.errors)return extractMessage(r.errors);if(r.errs)return extractMessage(r.errs);if(r.reason)return extractMessage(r.reason);if(r.reasons)return extractMessage(r.reasons);if(r.issue)return extractMessage(r.issue);if(r.issues)return extractMessage(r.issues);if(r.data)return extractMessage(r.data);try{return JSON.stringify(r)}catch(e){console.error("Error during extractMessage:"),console.error("Original Error: ",r),console.error("JSON.stringify Error:",e)}}return DEFAULT_MESSAGE};export const encodeError=(r,e)=>`${extractMessage(r)}${wrapCode(e)}`;export const decodeError=r=>{const e=extractMessage(r),{code:s,startsAt:t}=unwrapCode(e);return{message:t>0?e.slice(0,t):e,code:s}};export const isEncodedError=r=>decodeError(r).code!==DEFAULT_CODE;export const hasErrorCode=(r,e)=>null!==r&&(r===e||"object"==typeof r&&"code"in r&&r.code===e||decodeError(r).code===e);export const isDefaultErrorMessage=(r,e=!1)=>e?r===DEFAULT_MESSAGE:"string"==typeof r&&r.includes(DEFAULT_MESSAGE);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ZodError } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Attempts to extract a Zod error message from a ZodError instance. If unable to do so, it returns
|
|
4
|
+
* the default error message.
|
|
5
|
+
* @param error The ZodError instance to extract the message from.
|
|
6
|
+
* @returns The extracted error message or the default message.
|
|
7
|
+
*/
|
|
8
|
+
export declare const extractZodErrorMessage: (error: ZodError) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{DEFAULT_MESSAGE}from"../shared/constants.js";const __extractPathFromZodError=s=>s&&Array.isArray(s.issues)&&s.issues.length&&Array.isArray(s.issues[0].path)&&s.issues[0].path.length?s.issues[0].path.join("."):"Unknown path";export const extractZodErrorMessage=s=>s&&Array.isArray(s.issues)&&s.issues.length&&Array.isArray(s.issues[0].path)&&s.issues[0].message?`${s.issues[0].message} (${__extractPathFromZodError(s)})`:DEFAULT_MESSAGE;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { IErrorCode, IDecodedError } from './shared/types.js';
|
|
2
2
|
export { DEFAULT_CODE, DEFAULT_MESSAGE } from './shared/constants.js';
|
|
3
|
-
export {
|
|
3
|
+
export { extractMessage, encodeError, decodeError, isEncodedError, hasErrorCode, isDefaultErrorMessage, } from './error-handler/index.js';
|
|
4
4
|
export { type IExceptionRecord, Exception } from './exception/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";export{
|
|
1
|
+
export{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";export{extractMessage,encodeError,decodeError,isEncodedError,hasErrorCode,isDefaultErrorMessage}from"./error-handler/index.js";export{Exception}from"./exception/index.js";
|
package/dist/utils/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{CODE_WRAPPER,DEFAULT_CODE}from"../shared/constants.js";export const wrapCode=r=>`${CODE_WRAPPER.prefix}${r??DEFAULT_CODE}${CODE_WRAPPER.suffix}`;const __isEncodedError=r=>new RegExp(`${CODE_WRAPPER.prefix}.+${CODE_WRAPPER.suffix}$`).test(r),__isNumeric=r
|
|
1
|
+
import{CODE_WRAPPER,DEFAULT_CODE}from"../shared/constants.js";export const wrapCode=r=>`${CODE_WRAPPER.prefix}${r??DEFAULT_CODE}${CODE_WRAPPER.suffix}`;const __isEncodedError=r=>new RegExp(`${CODE_WRAPPER.prefix}.+${CODE_WRAPPER.suffix}$`).test(r),__isNumeric=r=>"string"==typeof r&&/^[-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(r);export const unwrapCode=r=>{if(__isEncodedError(r)){const E=r.lastIndexOf(CODE_WRAPPER.prefix),e=r.substring(E+CODE_WRAPPER.prefix.length,r.lastIndexOf(CODE_WRAPPER.suffix));return{code:__isNumeric(e)?Number(e):e,startsAt:E}}return{code:DEFAULT_CODE,startsAt:-1}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "error-message-utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"description": "The error-message-utils package simplifies error management in your web applications and RESTful APIs. It ensures consistent and scalable handling of error messages, saving you time and effort. Moreover, it gives you the ability to assign custom error codes so all possible cases can be handled accordingly.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/jesusgraterol/error-message-utils#readme",
|
|
39
39
|
"devDependencies": {
|
|
40
|
+
"@jest/globals": "30.3.0",
|
|
40
41
|
"@types/jest": "29.5.14",
|
|
41
42
|
"@types/node": "20.17.9",
|
|
42
43
|
"@typescript-eslint/eslint-plugin": "7.18.0",
|
|
@@ -50,7 +51,6 @@
|
|
|
50
51
|
"typescript": "5.7.2"
|
|
51
52
|
},
|
|
52
53
|
"dependencies": {
|
|
53
|
-
"@jest/globals": "30.3.0",
|
|
54
54
|
"zod": "4.3.6"
|
|
55
55
|
}
|
|
56
56
|
}
|