error-message-utils 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jesus Graterol
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # Error Message Utils
2
+
3
+ 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.
4
+
5
+
6
+
7
+
8
+
9
+ </br>
10
+
11
+ ## Getting Started
12
+
13
+ Install the package:
14
+ ```bash
15
+ $ npm install -S error-message-utils
16
+ ```
17
+
18
+
19
+
20
+
21
+
22
+ </br>
23
+
24
+ ## Usage
25
+
26
+ Encoding an error:
27
+ ```typescript
28
+ import { encodeError } from 'error-message-utils';
29
+
30
+ if (emailExists()) {
31
+ throw new Error(encodeError(
32
+ 'The provided email is already in use.',
33
+ 'EMAIL_EXISTS'
34
+ ));
35
+ // The provided email is already in use.{(EMAIL_EXISTS)}
36
+ }
37
+ ```
38
+
39
+ Decoding an error:
40
+ ```typescript
41
+ import { decodeError } from 'error-message-utils';
42
+
43
+ decodeError('The provided email is already in use.{(EMAIL_EXISTS)}');
44
+ // {
45
+ // message: 'The provided email is already in use.',
46
+ // code: EMAIL_EXISTS
47
+ // }
48
+ ```
49
+
50
+ Error messages can be extracted recursively from complex structures, including nested `cause` data properties from `Error` instances:
51
+ ```typescript
52
+ import { extractMessage } from 'error-message-utils';
53
+
54
+ extractMessage(new Error('Top level error', {
55
+ cause: new Error('First nested cause', {
56
+ cause: new Error('Second nested cause'),
57
+ }),
58
+ }));
59
+ // 'Top level error; [CAUSE]: First nested cause; [CAUSE]: Second nested cause'
60
+
61
+
62
+ extractMessage({
63
+ message: {
64
+ err: {
65
+ message: 'This error message is nested deeply!'
66
+ }
67
+ }
68
+ });
69
+ // 'This error message is nested deeply!'
70
+ ```
71
+
72
+
73
+
74
+ <br/>
75
+
76
+ ## Built With
77
+
78
+ - JavaScript / TypeScript
79
+
80
+
81
+
82
+
83
+ <br/>
84
+
85
+ ## Running the Tests
86
+
87
+ ```bash
88
+ $ npm run test:unit
89
+ ```
90
+
91
+
92
+
93
+
94
+
95
+ <br/>
96
+
97
+ ## License
98
+
99
+ [MIT](https://choosealicense.com/licenses/mit/)
100
+
101
+
102
+
103
+
104
+
105
+ <br/>
106
+
107
+ ## Acknowledgments
108
+
109
+ - ...
110
+
111
+
112
+
113
+
114
+
115
+ <br/>
116
+
117
+ ## @TODOS
118
+
119
+ - [ ] ...
120
+
121
+
122
+
123
+
124
+
125
+ <br/>
126
+
127
+ ## Deployment
128
+
129
+ Install dependencies:
130
+ ```bash
131
+ $ npm install
132
+ ```
133
+
134
+
135
+ Build the library:
136
+ ```bash
137
+ $ npm start
138
+ ```
139
+
140
+
141
+ Publish to `npm`:
142
+ ```bash
143
+ $ npm publish
144
+ ```
145
+
146
+
@@ -0,0 +1 @@
1
+ export * from './message/index.js';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export*from"./message/index.js";
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export * from './message.js';
@@ -0,0 +1 @@
1
+ export*from"./types.js";export*from"./message.js";
@@ -0,0 +1,23 @@
1
+ import { IErrorCode, IDecodedError } from './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
+ export { extractMessage, encodeError, decodeError, };
@@ -0,0 +1 @@
1
+ import{DEFAULT_MESSAGE,wrapCode,unwrapCode}from"./message.utils.js";const extractMessage=e=>{if("string"==typeof e&&e.length)return e;if(e instanceof Error&&e.message)return e.cause?e.message+"; [CAUSE]: "+extractMessage(e.cause):e.message;if(e&&"object"==typeof e){if(e.message)return extractMessage(e.message);if(e.msg)return extractMessage(e.msg);if(e.error)return extractMessage(e.error);if(e.err)return extractMessage(e.err);if(e.errors)return extractMessage(e.errors);if(e.errs)return extractMessage(e.errs);try{return JSON.stringify(e)}catch(r){console.error("Error during extractMessage:"),console.error("Original Error: ",e),console.error("JSON.stringify Error:",r)}}return DEFAULT_MESSAGE},encodeError=(r,e)=>""+extractMessage(r)+wrapCode(e),decodeError=r=>{var r=extractMessage(r),{code:e,startsAt:s}=unwrapCode(r);return{message:0<s?r.slice(0,s):r,code:e}};export{extractMessage,encodeError,decodeError};
@@ -0,0 +1,18 @@
1
+ import { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode } from './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, };
@@ -0,0 +1 @@
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??DEFAULT_CODE)+CODE_WRAPPER.suffix,__isEncodedError=e=>new RegExp(`${CODE_WRAPPER.prefix}.+${CODE_WRAPPER.suffix}$`).test(e),__isNumeric=e=>!Number.isNaN(Number.parseFloat(e)),unwrapCode=e=>{var r;return __isEncodedError(e)?(r=e.indexOf(CODE_WRAPPER.prefix),e=e.substring(r+2,e.lastIndexOf(CODE_WRAPPER.suffix)),{code:__isNumeric(e)?Number(e):e,startsAt:r}):{code:-1,startsAt:-1}};export{CODE_WRAPPER,DEFAULT_MESSAGE,DEFAULT_CODE,wrapCode,unwrapCode};
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Error Code Wrapper
3
+ * When an error code is inserted into a message (encoding a message), it must be wrapped first so
4
+ * it can be decoded later.
5
+ */
6
+ interface IErrorCodeWrapper {
7
+ prefix: string;
8
+ suffix: string;
9
+ }
10
+ /**
11
+ * Error Code
12
+ * The error's code that is inserted when encoding an error. If none is provided or none can be
13
+ * extracted, it defaults to -1.
14
+ */
15
+ type IErrorCode = string | number;
16
+ /**
17
+ * Unwrapped Error Code
18
+ * In order to decode an error, the code must be first unwrapped.
19
+ */
20
+ interface IUnwrappedErrorCode {
21
+ code: IErrorCode;
22
+ startsAt: number;
23
+ }
24
+ /**
25
+ * Decoded Error
26
+ * The object obtained when an error is decoded. Keep in mind that if the error message or the code
27
+ * cannot be extracted for any reason, the default values will be set instead.
28
+ */
29
+ interface IDecodedError {
30
+ message: string;
31
+ code: IErrorCode;
32
+ }
33
+ export { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode, IDecodedError, };
File without changes
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "error-message-utils",
3
+ "version": "1.0.0",
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
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "start": "ts-lib-builder --tsconfigPath=tsconfig.build.json",
10
+ "test": "echo \"Error: tests are executed with npm run test:(integration|unit)\" && exit 1",
11
+ "test:integration": "jest --config=jest.test-integration.config.json --no-cache",
12
+ "test:unit": "jest --config=jest.test-unit.config.json --no-cache"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/jesusgraterol/error-message-utils.git"
17
+ },
18
+ "keywords": [
19
+ "error",
20
+ "error-management",
21
+ "encoder",
22
+ "decoder",
23
+ "error-encoder",
24
+ "error-decoder",
25
+ "utils",
26
+ "utilities",
27
+ "error-utils",
28
+ "error-utilities",
29
+ "stderr",
30
+ "standard-error",
31
+ "error-handling"
32
+ ],
33
+ "author": "Jesus Graterol",
34
+ "license": "MIT",
35
+ "bugs": {
36
+ "url": "https://github.com/jesusgraterol/error-message-utils/issues"
37
+ },
38
+ "homepage": "https://github.com/jesusgraterol/error-message-utils#readme",
39
+ "devDependencies": {
40
+ "@types/jest": "^29.5.12",
41
+ "@types/node": "^20.12.9",
42
+ "@typescript-eslint/eslint-plugin": "^7.8.0",
43
+ "@typescript-eslint/parser": "^7.8.0",
44
+ "eslint-config-airbnb-typescript": "^18.0.0",
45
+ "jest": "^29.7.0",
46
+ "ts-jest": "^29.1.2",
47
+ "ts-lib-builder": "^1.0.2",
48
+ "typescript": "^5.4.5"
49
+ }
50
+ }