error-message-utils 1.0.2 → 1.1.0

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 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
- export * from './shared/types.js';
2
- export * from './error/index.js';
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
- export*from"./shared/types.js";export*from"./error/index.js";
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,4 +1,4 @@
1
- import { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode } from '../shared/index.js';
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.0.2",
3
+ "version": "1.1.0",
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",
@@ -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, };
@@ -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};
@@ -1 +0,0 @@
1
- export * from './error.js';
@@ -1 +0,0 @@
1
- export*from"./error.js";
@@ -1 +0,0 @@
1
- export * from './types.js';
@@ -1 +0,0 @@
1
- export*from"./types.js";
@@ -1 +0,0 @@
1
- export * from './utils.js';
@@ -1 +0,0 @@
1
- export*from"./utils.js";