error-message-utils 1.2.1 → 1.2.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 +31 -1
- 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 +5 -42
- package/dist/index.js +1 -1
- package/package.json +2 -1
- /package/dist/utils/{utils.d.ts → index.d.ts} +0 -0
- /package/dist/utils/{utils.js → index.js} +0 -0
package/README.md
CHANGED
|
@@ -117,7 +117,7 @@ isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
|
|
|
117
117
|
In some cases, you may want to check whether the extracted error matches the default message provided by this package:
|
|
118
118
|
|
|
119
119
|
```typescript
|
|
120
|
-
import { isDefaultErrorMessage} from 'error-message-utils';
|
|
120
|
+
import { isDefaultErrorMessage } from 'error-message-utils';
|
|
121
121
|
|
|
122
122
|
const DEFAULT_MESSAGE: string = 'The error message could not be extracted, check the logs for more information.';
|
|
123
123
|
|
|
@@ -134,6 +134,22 @@ isDefaultErrorMessage(`${DEFAULT_MESSAGE} and something else...`, true);
|
|
|
134
134
|
|
|
135
135
|
|
|
136
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
|
+
|
|
137
153
|
<br/>
|
|
138
154
|
|
|
139
155
|
## Types
|
|
@@ -156,6 +172,20 @@ type IDecodedError = {
|
|
|
156
172
|
```
|
|
157
173
|
|
|
158
174
|
|
|
175
|
+
<br/>
|
|
176
|
+
|
|
177
|
+
## Constants
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
// the default message if none can be extracted
|
|
181
|
+
const DEFAULT_MESSAGE: string =
|
|
182
|
+
'The error message could not be extracted, check the logs for more information.';
|
|
183
|
+
|
|
184
|
+
// the default code if none was provided or could not be extracted
|
|
185
|
+
const DEFAULT_CODE: IErrorCode = -1;
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
|
|
159
189
|
|
|
160
190
|
<br/>
|
|
161
191
|
|
|
@@ -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,45 +1,8 @@
|
|
|
1
1
|
import { IErrorCode, IDecodedError } from './shared/types.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { DEFAULT_CODE, DEFAULT_MESSAGE } from './shared/constants.js';
|
|
3
|
+
import { decodeError, encodeError, extractMessage, isDefaultErrorMessage, isEncodedError } from './error-handler/index.js';
|
|
4
|
+
import { Exception } from './exception/index.js';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
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.
|
|
6
|
+
* Module exports
|
|
10
7
|
*/
|
|
11
|
-
|
|
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, };
|
|
8
|
+
export { type IErrorCode, type IDecodedError, DEFAULT_MESSAGE, DEFAULT_CODE, extractMessage, encodeError, decodeError, isEncodedError, isDefaultErrorMessage, Exception, };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";import{decodeError,encodeError,extractMessage,isDefaultErrorMessage,isEncodedError}from"./error-handler/index.js";import{Exception}from"./exception/index.js";export{DEFAULT_MESSAGE,DEFAULT_CODE,extractMessage,encodeError,decodeError,isEncodedError,isDefaultErrorMessage,Exception};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "error-message-utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.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",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"typescript": "5.7.2"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
+
"@jest/globals": "30.3.0",
|
|
53
54
|
"zod": "4.3.6"
|
|
54
55
|
}
|
|
55
56
|
}
|
|
File without changes
|
|
File without changes
|