error-message-utils 1.0.2 → 1.1.1
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 +29 -0
- package/dist/index.d.ts +29 -2
- package/dist/index.js +1 -1
- package/dist/shared/types.d.ts +7 -7
- package/dist/utils/utils.d.ts +1 -1
- package/package.json +6 -6
- package/dist/error/error.d.ts +0 -23
- package/dist/error/error.js +0 -1
- package/dist/error/index.d.ts +0 -1
- package/dist/error/index.js +0 -1
- package/dist/shared/index.d.ts +0 -1
- package/dist/shared/index.js +0 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ $ npm install -S error-message-utils
|
|
|
24
24
|
## Usage
|
|
25
25
|
|
|
26
26
|
Encoding an error:
|
|
27
|
+
|
|
27
28
|
```typescript
|
|
28
29
|
import { encodeError } from 'error-message-utils';
|
|
29
30
|
|
|
@@ -36,7 +37,11 @@ if (emailExists()) {
|
|
|
36
37
|
}
|
|
37
38
|
```
|
|
38
39
|
|
|
40
|
+
|
|
41
|
+
<br/>
|
|
42
|
+
|
|
39
43
|
Decoding an error:
|
|
44
|
+
|
|
40
45
|
```typescript
|
|
41
46
|
import { decodeError } from 'error-message-utils';
|
|
42
47
|
|
|
@@ -47,7 +52,11 @@ decodeError('The provided email is already in use.{(EMAIL_EXISTS)}');
|
|
|
47
52
|
// }
|
|
48
53
|
```
|
|
49
54
|
|
|
55
|
+
|
|
56
|
+
<br/>
|
|
57
|
+
|
|
50
58
|
Error messages can be extracted recursively from complex structures, including nested `cause` data properties from `Error` instances:
|
|
59
|
+
|
|
51
60
|
```typescript
|
|
52
61
|
import { extractMessage } from 'error-message-utils';
|
|
53
62
|
|
|
@@ -70,6 +79,26 @@ extractMessage({
|
|
|
70
79
|
```
|
|
71
80
|
|
|
72
81
|
|
|
82
|
+
<br/>
|
|
83
|
+
|
|
84
|
+
Identifying encoded errors:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { isEncodedError, encodeError } from 'error-message-utils';
|
|
88
|
+
|
|
89
|
+
isEncodedError('Some random unencoded error');
|
|
90
|
+
// false
|
|
91
|
+
|
|
92
|
+
isEncodedError(new Error('Some random unencoded error'));
|
|
93
|
+
// false
|
|
94
|
+
|
|
95
|
+
isEncodedError(encodeError('Some unknown error.', 'NASTY_ERROR'));
|
|
96
|
+
// true
|
|
97
|
+
|
|
98
|
+
isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
|
|
99
|
+
// true
|
|
100
|
+
```
|
|
101
|
+
|
|
73
102
|
|
|
74
103
|
<br/>
|
|
75
104
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { IErrorCode, IDecodedError } from './shared/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Attempts to extract an error message from an error that could be anything. If it fails to do so,
|
|
4
|
+
* it returns the default message.
|
|
5
|
+
* @param error
|
|
6
|
+
* @returns string
|
|
7
|
+
*/
|
|
8
|
+
declare const extractMessage: (error: any) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Given an error in any format, it extracts the message and inserts the code at the end.
|
|
11
|
+
* @param error
|
|
12
|
+
* @param code
|
|
13
|
+
* @returns string
|
|
14
|
+
*/
|
|
15
|
+
declare const encodeError: (error: any, code: IErrorCode) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Given an error, it will extract the encoded message and attempt to decode it. If successful,
|
|
18
|
+
* it separates the error message from the code so it can be shown directly to the user.
|
|
19
|
+
* @param error
|
|
20
|
+
* @returns IDecodedError
|
|
21
|
+
*/
|
|
22
|
+
declare const decodeError: (error: any) => IDecodedError;
|
|
23
|
+
/**
|
|
24
|
+
* Determines if a given error (in any format) is an error encoded by this package.
|
|
25
|
+
* @param error
|
|
26
|
+
* @returns boolean
|
|
27
|
+
*/
|
|
28
|
+
declare const isEncodedError: (error: any) => boolean;
|
|
29
|
+
export { IErrorCode, IDecodedError, extractMessage, encodeError, decodeError, isEncodedError, };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import{DEFAULT_MESSAGE,DEFAULT_CODE,wrapCode,unwrapCode}from"./utils/utils.js";const extractMessage=r=>{if("string"==typeof r&&r.length)return 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);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;export{extractMessage,encodeError,decodeError,isEncodedError};
|
package/dist/shared/types.d.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
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
|
-
|
|
6
|
+
type IErrorCodeWrapper = {
|
|
7
7
|
prefix: string;
|
|
8
8
|
suffix: string;
|
|
9
|
-
}
|
|
9
|
+
};
|
|
10
10
|
/**
|
|
11
11
|
* Error Code
|
|
12
12
|
* The error's code that is inserted when encoding an error. If none is provided or none can be
|
|
@@ -17,17 +17,17 @@ type IErrorCode = string | number;
|
|
|
17
17
|
* Unwrapped Error Code
|
|
18
18
|
* In order to decode an error, the code must be first unwrapped.
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
type IUnwrappedErrorCode = {
|
|
21
21
|
code: IErrorCode;
|
|
22
22
|
startsAt: number;
|
|
23
|
-
}
|
|
23
|
+
};
|
|
24
24
|
/**
|
|
25
25
|
* Decoded Error
|
|
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
|
-
|
|
29
|
+
type IDecodedError = {
|
|
30
30
|
message: string;
|
|
31
31
|
code: IErrorCode;
|
|
32
|
-
}
|
|
33
|
-
export { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode, IDecodedError, };
|
|
32
|
+
};
|
|
33
|
+
export type { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode, IDecodedError, };
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode } from '../shared/
|
|
1
|
+
import { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode } from '../shared/types.js';
|
|
2
2
|
declare const CODE_WRAPPER: IErrorCodeWrapper;
|
|
3
3
|
declare const DEFAULT_MESSAGE: string;
|
|
4
4
|
declare const DEFAULT_CODE: IErrorCode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "error-message-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
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",
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
"homepage": "https://github.com/jesusgraterol/error-message-utils#readme",
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/jest": "^29.5.12",
|
|
41
|
-
"@types/node": "^20.
|
|
42
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
43
|
-
"@typescript-eslint/parser": "^7.
|
|
41
|
+
"@types/node": "^20.14.10",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^7.16.0",
|
|
43
|
+
"@typescript-eslint/parser": "^7.16.0",
|
|
44
44
|
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
45
45
|
"jest": "^29.7.0",
|
|
46
|
-
"ts-jest": "^29.
|
|
46
|
+
"ts-jest": "^29.2.0",
|
|
47
47
|
"ts-lib-builder": "^1.0.3",
|
|
48
|
-
"typescript": "^5.
|
|
48
|
+
"typescript": "^5.5.3"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/dist/error/error.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { IErrorCode, IDecodedError } from '../shared/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* Attempts to extract an error message from an error that could be anything. If it fails to do so,
|
|
4
|
-
* it returns the default message.
|
|
5
|
-
* @param error
|
|
6
|
-
* @returns string
|
|
7
|
-
*/
|
|
8
|
-
declare const extractMessage: (error: any) => string;
|
|
9
|
-
/**
|
|
10
|
-
* Given an error in any format, it extracts the message and inserts the code at the end.
|
|
11
|
-
* @param error
|
|
12
|
-
* @param code
|
|
13
|
-
* @returns string
|
|
14
|
-
*/
|
|
15
|
-
declare const encodeError: (error: any, code: IErrorCode) => string;
|
|
16
|
-
/**
|
|
17
|
-
* Given an error, it will extract the encoded message and attempt to decode it. If successful,
|
|
18
|
-
* it separates the error message from the code so it can be shown directly to the user.
|
|
19
|
-
* @param error
|
|
20
|
-
* @returns IDecodedError
|
|
21
|
-
*/
|
|
22
|
-
declare const decodeError: (error: any) => IDecodedError;
|
|
23
|
-
export { extractMessage, encodeError, decodeError, };
|
package/dist/error/error.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{DEFAULT_MESSAGE,wrapCode,unwrapCode}from"../utils/utils.js";const extractMessage=r=>{if("string"==typeof r&&r.length)return 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);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}};export{extractMessage,encodeError,decodeError};
|
package/dist/error/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './error.js';
|
package/dist/error/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export*from"./error.js";
|
package/dist/shared/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './types.js';
|
package/dist/shared/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export*from"./types.js";
|
package/dist/utils/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './utils.js';
|
package/dist/utils/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export*from"./utils.js";
|