error-message-utils 1.1.6 → 1.2.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 CHANGED
@@ -17,7 +17,7 @@ npm i -S error-message-utils
17
17
 
18
18
  ### Examples
19
19
 
20
- Encoding an error:
20
+ Encode an error:
21
21
 
22
22
  ```typescript
23
23
  import { encodeError } from 'error-message-utils';
@@ -34,7 +34,7 @@ if (emailExists()) {
34
34
 
35
35
  <br/>
36
36
 
37
- Decoding an error:
37
+ Decode an error:
38
38
 
39
39
  ```typescript
40
40
  import { decodeError } from 'error-message-utils';
@@ -75,7 +75,25 @@ extractMessage({
75
75
 
76
76
  <br/>
77
77
 
78
- Identifying encoded errors:
78
+ Extract detailed error messages from [Zod](https://zod.dev/basics) parsing errors.
79
+
80
+ ```typescript
81
+ import { z } from "zod";
82
+ import { extractMessage } from 'error-message-utils';
83
+
84
+ z.object({ name: z.string() }).parse({ name: 123 });
85
+ // Invalid input: expected string, received number (name)
86
+
87
+ z.object({
88
+ name: z.object({ age: z.number() })
89
+ }).parse({ name: { age: 'not a number' } })
90
+ // Invalid input: expected string, received number (someDict.innerList.0.someProp)
91
+ ```
92
+
93
+
94
+ <br/>
95
+
96
+ Identify encoded errors:
79
97
 
80
98
  ```typescript
81
99
  import { isEncodedError, encodeError } from 'error-message-utils';
package/dist/index.d.ts CHANGED
@@ -1,36 +1,45 @@
1
1
  import { IErrorCode, IDecodedError } from './shared/types.js';
2
+ /**------------------------------------------------------------------------------------------------
3
+ * General errors
4
+ -------------------------------------------------------------------------------------------------*/
2
5
  /**
3
6
  * Attempts to extract an error message from an error that could be anything. If it fails to do so,
4
7
  * it returns the default message.
5
- * @param error
6
- * @returns string
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.
7
10
  */
8
11
  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;
12
+ /**------------------------------------------------------------------------------------------------
13
+ * Encoding
14
+ -------------------------------------------------------------------------------------------------*/
16
15
  /**
17
16
  * Given an error in any format, it extracts the message and inserts the code at the end.
18
- * @param error
19
- * @param code
20
- * @returns string
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.
21
20
  */
22
21
  declare const encodeError: (error: any, code: IErrorCode) => string;
23
22
  /**
24
23
  * Given an error, it will extract the encoded message and attempt to decode it. If successful,
25
24
  * it separates the error message from the code so it can be shown directly to the user.
26
- * @param error
27
- * @returns IDecodedError
25
+ * @param error The error to be decoded, can be of any type.
26
+ * @returns The decoded error, containing the message and the code.
28
27
  */
29
28
  declare const decodeError: (error: any) => IDecodedError;
30
29
  /**
31
30
  * Determines if a given error (in any format) is an error encoded by this package.
32
- * @param error
33
- * @returns boolean
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.
34
33
  */
35
34
  declare const isEncodedError: (error: any) => boolean;
36
- export { type IErrorCode, type IDecodedError, extractMessage, isDefaultErrorMessage, encodeError, decodeError, isEncodedError, };
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, };
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},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};
1
+ import{ZodError}from"zod";import{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";import{wrapCode,unwrapCode}from"./utils/utils.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,4 @@
1
+ import { IErrorCode, IErrorCodeWrapper } from './types.js';
2
+ export declare const CODE_WRAPPER: IErrorCodeWrapper;
3
+ export declare const DEFAULT_MESSAGE: string;
4
+ export declare const DEFAULT_CODE: IErrorCode;
@@ -0,0 +1 @@
1
+ export const CODE_WRAPPER={prefix:"{(",suffix:")}"};export const DEFAULT_MESSAGE="The error message could not be extracted, check the logs for more information.";export const DEFAULT_CODE=-1;
@@ -3,7 +3,7 @@
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
- type IErrorCodeWrapper = {
6
+ export type IErrorCodeWrapper = {
7
7
  prefix: string;
8
8
  suffix: string;
9
9
  };
@@ -12,12 +12,12 @@ type IErrorCodeWrapper = {
12
12
  * The code that is inserted when encoding an error. If none is provided or none can be extracted,
13
13
  * it defaults to -1.
14
14
  */
15
- type IErrorCode = string | number;
15
+ export type IErrorCode = string | number;
16
16
  /**
17
17
  * Unwrapped Error Code
18
18
  * In order to decode an error, the code must be first unwrapped.
19
19
  */
20
- type IUnwrappedErrorCode = {
20
+ export type IUnwrappedErrorCode = {
21
21
  code: IErrorCode;
22
22
  startsAt: number;
23
23
  };
@@ -26,8 +26,7 @@ type IUnwrappedErrorCode = {
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
- type IDecodedError = {
29
+ export type IDecodedError = {
30
30
  message: string;
31
31
  code: IErrorCode;
32
32
  };
33
- export type { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode, IDecodedError };
@@ -1,18 +1,14 @@
1
- import { IErrorCodeWrapper, IErrorCode, IUnwrappedErrorCode } from '../shared/types.js';
2
- declare const CODE_WRAPPER: IErrorCodeWrapper;
3
- declare const DEFAULT_MESSAGE: string;
4
- declare const DEFAULT_CODE: IErrorCode;
1
+ import { IErrorCode, IUnwrappedErrorCode } from '../shared/types.js';
5
2
  /**
6
3
  * Wraps a given error code. If none is provided, it wraps the default code.
7
- * @param code
8
- * @returns string
4
+ * @param code The error code to wrap.
5
+ * @returns A string containing the wrapped error code.
9
6
  */
10
- declare const wrapCode: (code: IErrorCode) => string;
7
+ export declare const wrapCode: (code: IErrorCode) => string;
11
8
  /**
12
9
  * Verifies if a message is an encoded error and if so, attempts to extract the code.
13
10
  * If unsuccessful, both code and startsAt values will be -1.
14
- * @param message
15
- * @returns IUnwrappedErrorCode
11
+ * @param message The message to unwrap.
12
+ * @returns An object containing the unwrapped error code and its starting position.
16
13
  */
17
- declare const unwrapCode: (message: string) => IUnwrappedErrorCode;
18
- export { CODE_WRAPPER, DEFAULT_MESSAGE, DEFAULT_CODE, wrapCode, unwrapCode, };
14
+ export declare const unwrapCode: (message: string) => IUnwrappedErrorCode;
@@ -1 +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??-1}${CODE_WRAPPER.suffix}`,__isEncodedError=e=>new RegExp(`${CODE_WRAPPER.prefix}.+${CODE_WRAPPER.suffix}$`).test(e),__isNumeric=e=>!Number.isNaN(Number.parseFloat(e)),unwrapCode=e=>{if(__isEncodedError(e)){const r=e.lastIndexOf(CODE_WRAPPER.prefix),E=e.substring(r+2,e.lastIndexOf(CODE_WRAPPER.suffix));return{code:__isNumeric(E)?Number(E):E,startsAt:r}}return{code:-1,startsAt:-1}};export{CODE_WRAPPER,DEFAULT_MESSAGE,DEFAULT_CODE,wrapCode,unwrapCode};
1
+ import{CODE_WRAPPER,DEFAULT_CODE}from"../shared/constants.js";export const wrapCode=r=>`${CODE_WRAPPER.prefix}${r??DEFAULT_CODE}${CODE_WRAPPER.suffix}`;const __isEncodedError=r=>new RegExp(`${CODE_WRAPPER.prefix}.+${CODE_WRAPPER.suffix}$`).test(r),__isNumeric=r=>!Number.isNaN(Number.parseFloat(r));export const unwrapCode=r=>{if(__isEncodedError(r)){const s=r.lastIndexOf(CODE_WRAPPER.prefix),e=r.substring(s+2,r.lastIndexOf(CODE_WRAPPER.suffix));return{code:__isNumeric(e)?Number(e):e,startsAt:s}}return{code:DEFAULT_CODE,startsAt:-1}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "error-message-utils",
3
- "version": "1.1.6",
3
+ "version": "1.2.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",
@@ -48,5 +48,8 @@
48
48
  "ts-jest": "29.2.5",
49
49
  "ts-lib-builder": "1.0.8",
50
50
  "typescript": "5.7.2"
51
+ },
52
+ "dependencies": {
53
+ "zod": "4.3.6"
51
54
  }
52
55
  }