error-message-utils 1.2.8 → 1.2.9

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
@@ -96,7 +96,7 @@ z.object({
96
96
  Identify encoded errors:
97
97
 
98
98
  ```typescript
99
- import { isEncodedError, encodeError, hasErrorCode } from 'error-message-utils';
99
+ import { isEncodedError, encodeError, isErrorCodeCarrier, hasErrorCode } from 'error-message-utils';
100
100
 
101
101
  isEncodedError('Some random unencoded error');
102
102
  // false
@@ -110,12 +110,14 @@ isEncodedError(encodeError('Some unknown error.', 'NASTY_ERROR'));
110
110
  isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
111
111
  // true
112
112
 
113
- hasErrorCode(new Error('Oops, something went wrong.'), 'MY_ERROR_CODE'); // false
114
- hasErrorCode(
115
- new Exception('Oops, something went wrong.', 'MY_ERROR_CODE'),
116
- 'MY_ERROR_CODE'
117
- );
118
- // true
113
+ const error = new Error('Oops, something went wrong.');
114
+ const exception = new Exception('Oops, something went wrong.', 'MY_ERROR_CODE');
115
+
116
+ isErrorCodeCarrier(error); // false
117
+ hasErrorCode(error, 'MY_ERROR_CODE'); // false
118
+
119
+ isErrorCodeCarrier(exception); // true
120
+ hasErrorCode(exception, 'MY_ERROR_CODE'); // true
119
121
  ```
120
122
 
121
123
 
@@ -192,6 +194,15 @@ type IExceptionRecord = {
192
194
  code: IErrorCode;
193
195
  data: unknown | null;
194
196
  };
197
+
198
+ /**
199
+ * Error Code Carrier
200
+ * An object that carries an error code, typically used to identify errors programmatically.
201
+ */
202
+ type IErrorCodeCarrier = {
203
+ code: IErrorCode;
204
+ message: string;
205
+ } & Record<string, unknown>;
195
206
  ```
196
207
 
197
208
 
@@ -1,4 +1,4 @@
1
- import { IErrorCode, IDecodedError } from '../shared/types.js';
1
+ import type { IErrorCode, IDecodedError, IErrorCodeCarrier } from '../shared/types.js';
2
2
  /**
3
3
  * General errors
4
4
  */
@@ -35,6 +35,12 @@ export declare const isEncodedError: (error: any) => boolean;
35
35
  /**
36
36
  * Misc helpers
37
37
  */
38
+ /**
39
+ * Determines whether an unknown value has an inspectable error code property.
40
+ * @param error The unknown value to inspect.
41
+ * @returns True when the value can carry an Exception-style code.
42
+ */
43
+ export declare const isErrorCodeCarrier: (error: unknown) => error is IErrorCodeCarrier;
38
44
  /**
39
45
  * Checks if the given error matches the specified error code.
40
46
  * @param error The error to be checked, can be of any type.
@@ -1 +1 @@
1
- import{ZodError}from"zod";import{DEFAULT_CODE,DEFAULT_MESSAGE}from"../shared/constants.js";import{wrapCode,unwrapCode}from"../utils/index.js";import{extractZodErrorMessage}from"./utilities.js";export const 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};export const encodeError=(r,e)=>`${extractMessage(r)}${wrapCode(e)}`;export const decodeError=r=>{const e=extractMessage(r),{code:s,startsAt:t}=unwrapCode(e);return{message:t>0?e.slice(0,t):e,code:s}};export const isEncodedError=r=>decodeError(r).code!==DEFAULT_CODE;export const hasErrorCode=(r,e)=>null!==r&&(r===e||"object"==typeof r&&"code"in r&&r.code===e||decodeError(r).code===e);export const isDefaultErrorMessage=(r,e=!1)=>e?r===DEFAULT_MESSAGE:"string"==typeof r&&r.includes(DEFAULT_MESSAGE);
1
+ import{ZodError}from"zod";import{DEFAULT_CODE,DEFAULT_MESSAGE}from"../shared/constants.js";import{wrapCode,unwrapCode}from"../utils/index.js";import{extractZodErrorMessage}from"./utilities.js";export const 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};export const encodeError=(r,e)=>`${extractMessage(r)}${wrapCode(e)}`;export const decodeError=r=>{const e=extractMessage(r),{code:s,startsAt:t}=unwrapCode(e);return{message:t>0?e.slice(0,t):e,code:s}};export const isEncodedError=r=>decodeError(r).code!==DEFAULT_CODE;export const isErrorCodeCarrier=r=>"object"==typeof r&&null!==r&&"code"in r&&("string"==typeof r.code||"number"==typeof r.code);export const hasErrorCode=(r,e)=>null!==r&&(r===e||isErrorCodeCarrier(r)&&r.code===e||decodeError(r).code===e);export const isDefaultErrorMessage=(r,e=!1)=>e?r===DEFAULT_MESSAGE:"string"==typeof r&&r.includes(DEFAULT_MESSAGE);
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { IErrorCode, IDecodedError } from './shared/types.js';
1
+ export { IErrorCode, IDecodedError, IErrorCodeCarrier } from './shared/types.js';
2
2
  export { DEFAULT_CODE, DEFAULT_MESSAGE } from './shared/constants.js';
3
- export { extractMessage, encodeError, decodeError, isEncodedError, hasErrorCode, isDefaultErrorMessage, } from './error-handler/index.js';
3
+ export { extractMessage, encodeError, decodeError, isEncodedError, isErrorCodeCarrier, hasErrorCode, isDefaultErrorMessage, } from './error-handler/index.js';
4
4
  export { type IExceptionRecord, Exception } from './exception/index.js';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";export{extractMessage,encodeError,decodeError,isEncodedError,hasErrorCode,isDefaultErrorMessage}from"./error-handler/index.js";export{Exception}from"./exception/index.js";
1
+ export{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";export{extractMessage,encodeError,decodeError,isEncodedError,isErrorCodeCarrier,hasErrorCode,isDefaultErrorMessage}from"./error-handler/index.js";export{Exception}from"./exception/index.js";
@@ -30,3 +30,12 @@ export type IDecodedError = {
30
30
  message: string;
31
31
  code: IErrorCode;
32
32
  };
33
+ /**
34
+ * Error Code Carrier
35
+ * An object that carries an error code, typically used to identify errors programmatically. Extra
36
+ * fields are allowed because provider and application errors often carry metadata.
37
+ */
38
+ export type IErrorCodeCarrier = {
39
+ code: IErrorCode;
40
+ message: string;
41
+ } & Record<string, unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "error-message-utils",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
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",