error-message-utils 1.1.6 → 1.2.2
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 +38 -4
- package/dist/error-handler/index.d.ts +45 -0
- package/dist/error-handler/index.js +1 -0
- package/dist/exception/index.d.ts +16 -0
- package/dist/exception/index.js +1 -0
- package/dist/index.d.ts +4 -33
- package/dist/index.js +1 -1
- package/dist/shared/constants.d.ts +4 -0
- package/dist/shared/constants.js +1 -0
- package/dist/shared/types.d.ts +4 -5
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.js +1 -0
- package/package.json +5 -1
- package/dist/utils/utils.d.ts +0 -18
- package/dist/utils/utils.js +0 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ npm i -S error-message-utils
|
|
|
17
17
|
|
|
18
18
|
### Examples
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
Encode an error:
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
23
|
import { encodeError } from 'error-message-utils';
|
|
@@ -34,7 +34,7 @@ if (emailExists()) {
|
|
|
34
34
|
|
|
35
35
|
<br/>
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
Decode an error:
|
|
38
38
|
|
|
39
39
|
```typescript
|
|
40
40
|
import { decodeError } from 'error-message-utils';
|
|
@@ -75,7 +75,25 @@ extractMessage({
|
|
|
75
75
|
|
|
76
76
|
<br/>
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
Extract detailed error messages from [Zod](https://zod.dev/basics) parsing errors.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { z } from "zod";
|
|
82
|
+
import { extractMessage } from 'error-message-utils';
|
|
83
|
+
|
|
84
|
+
z.object({ name: z.string() }).parse({ name: 123 });
|
|
85
|
+
// Invalid input: expected string, received number (name)
|
|
86
|
+
|
|
87
|
+
z.object({
|
|
88
|
+
name: z.object({ age: z.number() })
|
|
89
|
+
}).parse({ name: { age: 'not a number' } })
|
|
90
|
+
// Invalid input: expected string, received number (someDict.innerList.0.someProp)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
<br/>
|
|
95
|
+
|
|
96
|
+
Identify encoded errors:
|
|
79
97
|
|
|
80
98
|
```typescript
|
|
81
99
|
import { isEncodedError, encodeError } from 'error-message-utils';
|
|
@@ -99,7 +117,7 @@ isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
|
|
|
99
117
|
In some cases, you may want to check whether the extracted error matches the default message provided by this package:
|
|
100
118
|
|
|
101
119
|
```typescript
|
|
102
|
-
import { isDefaultErrorMessage} from 'error-message-utils';
|
|
120
|
+
import { isDefaultErrorMessage } from 'error-message-utils';
|
|
103
121
|
|
|
104
122
|
const DEFAULT_MESSAGE: string = 'The error message could not be extracted, check the logs for more information.';
|
|
105
123
|
|
|
@@ -116,6 +134,22 @@ isDefaultErrorMessage(`${DEFAULT_MESSAGE} and something else...`, true);
|
|
|
116
134
|
|
|
117
135
|
|
|
118
136
|
|
|
137
|
+
<br/>
|
|
138
|
+
|
|
139
|
+
Improve code consistency by handling errors with the `Exception` utility class:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { Exception } from 'error-message-utils';
|
|
143
|
+
|
|
144
|
+
const exception = new Exception('Request failed', 'SOME_ERROR_CODE');
|
|
145
|
+
exception instanceof Error; // true
|
|
146
|
+
exception instanceof Exception; // true
|
|
147
|
+
exception.message; // "Request failed"
|
|
148
|
+
exception.code; // "SOME_ERROR_CODE"
|
|
149
|
+
exception.toString(); // "Request failed{(SOME_ERROR_CODE)}"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
|
|
119
153
|
<br/>
|
|
120
154
|
|
|
121
155
|
## Types
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { IErrorCode, IDecodedError } from '../shared/types.js';
|
|
2
|
+
/**------------------------------------------------------------------------------------------------
|
|
3
|
+
* General errors
|
|
4
|
+
-------------------------------------------------------------------------------------------------*/
|
|
5
|
+
/**
|
|
6
|
+
* Attempts to extract an error message from an error that could be anything. If it fails to do so,
|
|
7
|
+
* it returns the default message.
|
|
8
|
+
* @param error The error to extract the message from.
|
|
9
|
+
* @returns A string containing the extracted message or the default message if extraction fails.
|
|
10
|
+
*/
|
|
11
|
+
declare const extractMessage: (error: any) => string;
|
|
12
|
+
/**------------------------------------------------------------------------------------------------
|
|
13
|
+
* Encoding
|
|
14
|
+
-------------------------------------------------------------------------------------------------*/
|
|
15
|
+
/**
|
|
16
|
+
* Given an error in any format, it extracts the message and inserts the code at the end.
|
|
17
|
+
* @param error The error to be encoded, can be of any type.
|
|
18
|
+
* @param code The error code to be wrapped and appended to the message.
|
|
19
|
+
* @returns A string containing the encoded error message.
|
|
20
|
+
*/
|
|
21
|
+
declare const encodeError: (error: any, code: IErrorCode) => string;
|
|
22
|
+
/**
|
|
23
|
+
* Given an error, it will extract the encoded message and attempt to decode it. If successful,
|
|
24
|
+
* it separates the error message from the code so it can be shown directly to the user.
|
|
25
|
+
* @param error The error to be decoded, can be of any type.
|
|
26
|
+
* @returns The decoded error, containing the message and the code.
|
|
27
|
+
*/
|
|
28
|
+
declare const decodeError: (error: any) => IDecodedError;
|
|
29
|
+
/**
|
|
30
|
+
* Determines if a given error (in any format) is an error encoded by this package.
|
|
31
|
+
* @param error The error to be checked, can be of any type.
|
|
32
|
+
* @returns A boolean indicating whether the error is an encoded error or not.
|
|
33
|
+
*/
|
|
34
|
+
declare const isEncodedError: (error: any) => boolean;
|
|
35
|
+
/**------------------------------------------------------------------------------------------------
|
|
36
|
+
* Misc helpers
|
|
37
|
+
-------------------------------------------------------------------------------------------------*/
|
|
38
|
+
/**
|
|
39
|
+
* Verifies if a value matches the default error message used by this package.
|
|
40
|
+
* @param value The value to be checked.
|
|
41
|
+
* @param fullMatch Whether to check for an exact match or a partial match.
|
|
42
|
+
* @returns A boolean indicating whether the value matches the default error message.
|
|
43
|
+
*/
|
|
44
|
+
declare const isDefaultErrorMessage: (value: string, fullMatch?: boolean) => value is string;
|
|
45
|
+
export { type IErrorCode, type IDecodedError, extractMessage, encodeError, decodeError, isEncodedError, isDefaultErrorMessage, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ZodError}from"zod";import{DEFAULT_CODE,DEFAULT_MESSAGE}from"../shared/constants.js";import{wrapCode,unwrapCode}from"../utils/index.js";const __extractPathFromZodError=r=>r&&Array.isArray(r.issues)&&r.issues.length&&Array.isArray(r.issues[0].path)&&r.issues[0].path.length?r.issues[0].path.join("."):"Unknown path",__extractZodErrorMessage=r=>r&&Array.isArray(r.issues)&&r.issues.length&&Array.isArray(r.issues[0].path)&&r.issues[0].message?`${r.issues[0].message} (${__extractPathFromZodError(r)})`:DEFAULT_MESSAGE,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},encodeError=(r,e)=>`${extractMessage(r)}${wrapCode(e)}`,decodeError=r=>{const e=extractMessage(r),{code:s,startsAt:t}=unwrapCode(e);return{message:t>0?e.slice(0,t):e,code:s}},isEncodedError=r=>decodeError(r).code!==DEFAULT_CODE,isDefaultErrorMessage=(r,e=!1)=>e?r===DEFAULT_MESSAGE:"string"==typeof r&&r.includes(DEFAULT_MESSAGE);export{extractMessage,encodeError,decodeError,isEncodedError,isDefaultErrorMessage};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type IErrorCode } from '../shared/types.js';
|
|
2
|
+
export declare class Exception extends Error {
|
|
3
|
+
readonly code: IErrorCode;
|
|
4
|
+
constructor(error: unknown, code: IErrorCode);
|
|
5
|
+
/**
|
|
6
|
+
* Override the default toString method to return a formatted error message with the code.
|
|
7
|
+
* @returns A string representation of the error with the code.
|
|
8
|
+
*/
|
|
9
|
+
toString(): string;
|
|
10
|
+
/**
|
|
11
|
+
* Override the default behavior for type conversion to return a formatted error message with the code.
|
|
12
|
+
* @param hint The type hint for the conversion.
|
|
13
|
+
* @returns A string representation of the error with the code or null for other types.
|
|
14
|
+
*/
|
|
15
|
+
[Symbol.toPrimitive](hint: string): string | null;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{encodeError,extractMessage}from"../error-handler/index.js";export class Exception extends Error{code;constructor(e,r){super(extractMessage(e)),this.name="Exception",this.code=r}toString(){return encodeError(this.message,this.code)}[Symbol.toPrimitive](e){return"string"===e||"default"===e?this.toString():null}}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,36 +1,7 @@
|
|
|
1
1
|
import { IErrorCode, IDecodedError } from './shared/types.js';
|
|
2
|
+
import { decodeError, encodeError, extractMessage, isDefaultErrorMessage, isEncodedError } from './error-handler/index.js';
|
|
3
|
+
import { Exception } from './exception/index.js';
|
|
2
4
|
/**
|
|
3
|
-
*
|
|
4
|
-
* it returns the default message.
|
|
5
|
-
* @param error
|
|
6
|
-
* @returns string
|
|
5
|
+
* Module exports
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Verifies if a value matches the default error message used by this package.
|
|
11
|
-
* @param value
|
|
12
|
-
* @param fullMatch?
|
|
13
|
-
* @returns boolean
|
|
14
|
-
*/
|
|
15
|
-
declare const isDefaultErrorMessage: (value: string, fullMatch?: boolean) => value is string;
|
|
16
|
-
/**
|
|
17
|
-
* Given an error in any format, it extracts the message and inserts the code at the end.
|
|
18
|
-
* @param error
|
|
19
|
-
* @param code
|
|
20
|
-
* @returns string
|
|
21
|
-
*/
|
|
22
|
-
declare const encodeError: (error: any, code: IErrorCode) => string;
|
|
23
|
-
/**
|
|
24
|
-
* Given an error, it will extract the encoded message and attempt to decode it. If successful,
|
|
25
|
-
* it separates the error message from the code so it can be shown directly to the user.
|
|
26
|
-
* @param error
|
|
27
|
-
* @returns IDecodedError
|
|
28
|
-
*/
|
|
29
|
-
declare const decodeError: (error: any) => IDecodedError;
|
|
30
|
-
/**
|
|
31
|
-
* Determines if a given error (in any format) is an error encoded by this package.
|
|
32
|
-
* @param error
|
|
33
|
-
* @returns boolean
|
|
34
|
-
*/
|
|
35
|
-
declare const isEncodedError: (error: any) => boolean;
|
|
36
|
-
export { type IErrorCode, type IDecodedError, extractMessage, isDefaultErrorMessage, encodeError, decodeError, isEncodedError, };
|
|
7
|
+
export { type IErrorCode, type IDecodedError, extractMessage, encodeError, decodeError, isEncodedError, isDefaultErrorMessage, Exception, };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{decodeError,encodeError,extractMessage,isDefaultErrorMessage,isEncodedError}from"./error-handler/index.js";import{Exception}from"./exception/index.js";export{extractMessage,encodeError,decodeError,isEncodedError,isDefaultErrorMessage,Exception};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CODE_WRAPPER={prefix:"{(",suffix:")}"};export const DEFAULT_MESSAGE="The error message could not be extracted, check the logs for more information.";export const DEFAULT_CODE=-1;
|
package/dist/shared/types.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* When an error code is inserted into a message (encoding a message), it must be wrapped first so
|
|
4
4
|
* it can be decoded later.
|
|
5
5
|
*/
|
|
6
|
-
type IErrorCodeWrapper = {
|
|
6
|
+
export type IErrorCodeWrapper = {
|
|
7
7
|
prefix: string;
|
|
8
8
|
suffix: string;
|
|
9
9
|
};
|
|
@@ -12,12 +12,12 @@ type IErrorCodeWrapper = {
|
|
|
12
12
|
* The code that is inserted when encoding an error. If none is provided or none can be extracted,
|
|
13
13
|
* it defaults to -1.
|
|
14
14
|
*/
|
|
15
|
-
type IErrorCode = string | number;
|
|
15
|
+
export type IErrorCode = string | number;
|
|
16
16
|
/**
|
|
17
17
|
* Unwrapped Error Code
|
|
18
18
|
* In order to decode an error, the code must be first unwrapped.
|
|
19
19
|
*/
|
|
20
|
-
type IUnwrappedErrorCode = {
|
|
20
|
+
export type IUnwrappedErrorCode = {
|
|
21
21
|
code: IErrorCode;
|
|
22
22
|
startsAt: number;
|
|
23
23
|
};
|
|
@@ -26,8 +26,7 @@ type IUnwrappedErrorCode = {
|
|
|
26
26
|
* The object obtained when an error is decoded. Keep in mind that if the error message or the code
|
|
27
27
|
* cannot be extracted for any reason, the default values will be set instead.
|
|
28
28
|
*/
|
|
29
|
-
type IDecodedError = {
|
|
29
|
+
export type IDecodedError = {
|
|
30
30
|
message: string;
|
|
31
31
|
code: IErrorCode;
|
|
32
32
|
};
|
|
33
|
-
export type { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode, IDecodedError };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IErrorCode, IUnwrappedErrorCode } from '../shared/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a given error code. If none is provided, it wraps the default code.
|
|
4
|
+
* @param code The error code to wrap.
|
|
5
|
+
* @returns A string containing the wrapped error code.
|
|
6
|
+
*/
|
|
7
|
+
export declare const wrapCode: (code: IErrorCode) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Verifies if a message is an encoded error and if so, attempts to extract the code.
|
|
10
|
+
* If unsuccessful, both code and startsAt values will be -1.
|
|
11
|
+
* @param message The message to unwrap.
|
|
12
|
+
* @returns An object containing the unwrapped error code and its starting position.
|
|
13
|
+
*/
|
|
14
|
+
export declare const unwrapCode: (message: string) => IUnwrappedErrorCode;
|
|
@@ -0,0 +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=>!Number.isNaN(Number.parseFloat(r));export const unwrapCode=r=>{if(__isEncodedError(r)){const s=r.lastIndexOf(CODE_WRAPPER.prefix),e=r.substring(s+2,r.lastIndexOf(CODE_WRAPPER.suffix));return{code:__isNumeric(e)?Number(e):e,startsAt:s}}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.
|
|
3
|
+
"version": "1.2.2",
|
|
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",
|
|
@@ -48,5 +48,9 @@
|
|
|
48
48
|
"ts-jest": "29.2.5",
|
|
49
49
|
"ts-lib-builder": "1.0.8",
|
|
50
50
|
"typescript": "5.7.2"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@jest/globals": "30.3.0",
|
|
54
|
+
"zod": "4.3.6"
|
|
51
55
|
}
|
|
52
56
|
}
|
package/dist/utils/utils.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode } from '../shared/types.js';
|
|
2
|
-
declare const CODE_WRAPPER: IErrorCodeWrapper;
|
|
3
|
-
declare const DEFAULT_MESSAGE: string;
|
|
4
|
-
declare const DEFAULT_CODE: IErrorCode;
|
|
5
|
-
/**
|
|
6
|
-
* Wraps a given error code. If none is provided, it wraps the default code.
|
|
7
|
-
* @param code
|
|
8
|
-
* @returns string
|
|
9
|
-
*/
|
|
10
|
-
declare const wrapCode: (code: IErrorCode) => string;
|
|
11
|
-
/**
|
|
12
|
-
* Verifies if a message is an encoded error and if so, attempts to extract the code.
|
|
13
|
-
* If unsuccessful, both code and startsAt values will be -1.
|
|
14
|
-
* @param message
|
|
15
|
-
* @returns IUnwrappedErrorCode
|
|
16
|
-
*/
|
|
17
|
-
declare const unwrapCode: (message: string) => IUnwrappedErrorCode;
|
|
18
|
-
export { CODE_WRAPPER, DEFAULT_MESSAGE, DEFAULT_CODE, wrapCode, unwrapCode, };
|
package/dist/utils/utils.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const CODE_WRAPPER={prefix:"{(",suffix:")}"},DEFAULT_MESSAGE="The error message could not be extracted, check the logs for more information.",DEFAULT_CODE=-1,wrapCode=e=>`${CODE_WRAPPER.prefix}${e??-1}${CODE_WRAPPER.suffix}`,__isEncodedError=e=>new RegExp(`${CODE_WRAPPER.prefix}.+${CODE_WRAPPER.suffix}$`).test(e),__isNumeric=e=>!Number.isNaN(Number.parseFloat(e)),unwrapCode=e=>{if(__isEncodedError(e)){const r=e.lastIndexOf(CODE_WRAPPER.prefix),E=e.substring(r+2,e.lastIndexOf(CODE_WRAPPER.suffix));return{code:__isNumeric(E)?Number(E):E,startsAt:r}}return{code:-1,startsAt:-1}};export{CODE_WRAPPER,DEFAULT_MESSAGE,DEFAULT_CODE,wrapCode,unwrapCode};
|