error-message-utils 1.1.1 → 1.1.3
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 +44 -25
- package/dist/index.d.ts +8 -1
- package/dist/index.js +1 -1
- package/dist/shared/types.d.ts +2 -2
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -12,16 +12,10 @@ The `error-message-utils` package simplifies error management in your web applic
|
|
|
12
12
|
|
|
13
13
|
Install the package:
|
|
14
14
|
```bash
|
|
15
|
-
|
|
15
|
+
npm install -S error-message-utils
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
</br>
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
18
|
+
### Examples
|
|
25
19
|
|
|
26
20
|
Encoding an error:
|
|
27
21
|
|
|
@@ -102,40 +96,65 @@ isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
|
|
|
102
96
|
|
|
103
97
|
<br/>
|
|
104
98
|
|
|
105
|
-
|
|
99
|
+
In some cases, you may want to check whether the extracted error matches the default message provided by this package:
|
|
106
100
|
|
|
107
|
-
|
|
101
|
+
```typescript
|
|
102
|
+
import { isDefaultErrorMessage} from 'error-message-utils';
|
|
108
103
|
|
|
104
|
+
const DEFAULT_MESSAGE: string = 'The error message could not be extracted, check the logs for more information.';
|
|
109
105
|
|
|
106
|
+
isDefaultErrorMessage(DEFAULT_MESSAGE);
|
|
107
|
+
// true
|
|
110
108
|
|
|
109
|
+
isDefaultErrorMessage(`${DEFAULT_MESSAGE} and something else...`);
|
|
110
|
+
// false
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
isDefaultErrorMessage(`${DEFAULT_MESSAGE} and something else...`, true);
|
|
113
|
+
// true
|
|
114
|
+
```
|
|
113
115
|
|
|
114
|
-
## Running the Tests
|
|
115
116
|
|
|
116
|
-
```bash
|
|
117
|
-
$ npm run test:unit
|
|
118
|
-
```
|
|
119
117
|
|
|
120
118
|
|
|
119
|
+
<br/>
|
|
121
120
|
|
|
121
|
+
## Types
|
|
122
122
|
|
|
123
|
+
```typescript
|
|
124
|
+
/**
|
|
125
|
+
* Error Code
|
|
126
|
+
* The code that is inserted when encoding an error. If none is provided or none can be extracted, it defaults to -1.
|
|
127
|
+
*/
|
|
128
|
+
type IErrorCode = string | number;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Decoded Error
|
|
132
|
+
* The object obtained when an error is decoded. Keep in mind that if the error message or the code cannot be extracted for any reason, the default values will be set instead.
|
|
133
|
+
*/
|
|
134
|
+
type IDecodedError = {
|
|
135
|
+
message: string,
|
|
136
|
+
code: IErrorCode,
|
|
137
|
+
};
|
|
138
|
+
```
|
|
123
139
|
|
|
124
|
-
<br/>
|
|
125
140
|
|
|
126
|
-
## License
|
|
127
141
|
|
|
128
|
-
|
|
142
|
+
<br/>
|
|
129
143
|
|
|
144
|
+
## Built With
|
|
145
|
+
|
|
146
|
+
- TypeScript
|
|
130
147
|
|
|
131
148
|
|
|
132
149
|
|
|
133
150
|
|
|
134
151
|
<br/>
|
|
135
152
|
|
|
136
|
-
##
|
|
153
|
+
## Running the Tests
|
|
137
154
|
|
|
138
|
-
|
|
155
|
+
```bash
|
|
156
|
+
npm run test:unit
|
|
157
|
+
```
|
|
139
158
|
|
|
140
159
|
|
|
141
160
|
|
|
@@ -143,9 +162,9 @@ $ npm run test:unit
|
|
|
143
162
|
|
|
144
163
|
<br/>
|
|
145
164
|
|
|
146
|
-
##
|
|
165
|
+
## License
|
|
147
166
|
|
|
148
|
-
|
|
167
|
+
[MIT](https://choosealicense.com/licenses/mit/)
|
|
149
168
|
|
|
150
169
|
|
|
151
170
|
|
|
@@ -157,17 +176,17 @@ $ npm run test:unit
|
|
|
157
176
|
|
|
158
177
|
Install dependencies:
|
|
159
178
|
```bash
|
|
160
|
-
|
|
179
|
+
npm install
|
|
161
180
|
```
|
|
162
181
|
|
|
163
182
|
|
|
164
183
|
Build the library:
|
|
165
184
|
```bash
|
|
166
|
-
|
|
185
|
+
npm start
|
|
167
186
|
```
|
|
168
187
|
|
|
169
188
|
|
|
170
189
|
Publish to `npm`:
|
|
171
190
|
```bash
|
|
172
|
-
|
|
191
|
+
npm publish
|
|
173
192
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,13 @@ import { IErrorCode, IDecodedError } from './shared/types.js';
|
|
|
6
6
|
* @returns string
|
|
7
7
|
*/
|
|
8
8
|
declare const extractMessage: (error: any) => string;
|
|
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;
|
|
9
16
|
/**
|
|
10
17
|
* Given an error in any format, it extracts the message and inserts the code at the end.
|
|
11
18
|
* @param error
|
|
@@ -26,4 +33,4 @@ declare const decodeError: (error: any) => IDecodedError;
|
|
|
26
33
|
* @returns boolean
|
|
27
34
|
*/
|
|
28
35
|
declare const isEncodedError: (error: any) => boolean;
|
|
29
|
-
export { IErrorCode, IDecodedError, extractMessage, encodeError, decodeError, isEncodedError, };
|
|
36
|
+
export { type IErrorCode, type IDecodedError, extractMessage, isDefaultErrorMessage, encodeError, decodeError, isEncodedError, };
|
package/dist/index.js
CHANGED
|
@@ -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};
|
|
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},isDefaultErrorMessage=(r,e=!1)=>e?r===DEFAULT_MESSAGE:"string"==typeof r&&r.includes(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,isDefaultErrorMessage,encodeError,decodeError,isEncodedError};
|
package/dist/shared/types.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ type IErrorCodeWrapper = {
|
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
11
|
* Error Code
|
|
12
|
-
* The
|
|
13
|
-
*
|
|
12
|
+
* The code that is inserted when encoding an error. If none is provided or none can be extracted,
|
|
13
|
+
* it defaults to -1.
|
|
14
14
|
*/
|
|
15
15
|
type IErrorCode = string | number;
|
|
16
16
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "error-message-utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
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,14 +37,14 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/jesusgraterol/error-message-utils#readme",
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@types/jest": "^29.5.
|
|
41
|
-
"@types/node": "^20.
|
|
42
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
43
|
-
"@typescript-eslint/parser": "^7.
|
|
40
|
+
"@types/jest": "^29.5.14",
|
|
41
|
+
"@types/node": "^20.17.9",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
43
|
+
"@typescript-eslint/parser": "^7.18.0",
|
|
44
44
|
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
45
45
|
"jest": "^29.7.0",
|
|
46
|
-
"ts-jest": "^29.2.
|
|
47
|
-
"ts-lib-builder": "^1.0.
|
|
48
|
-
"typescript": "^5.
|
|
46
|
+
"ts-jest": "^29.2.5",
|
|
47
|
+
"ts-lib-builder": "^1.0.5",
|
|
48
|
+
"typescript": "^5.7.2"
|
|
49
49
|
}
|
|
50
50
|
}
|