error-message-utils 1.2.3 → 1.2.5

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
@@ -147,6 +147,12 @@ exception instanceof Exception; // true
147
147
  exception.message; // "Request failed"
148
148
  exception.code; // "SOME_ERROR_CODE"
149
149
  exception.toString(); // "Request failed{(SOME_ERROR_CODE)}"
150
+ exception.toRecord();
151
+ // {
152
+ // message: "Request failed",
153
+ // code: "SOME_ERROR_CODE",
154
+ // data: null,
155
+ // }
150
156
  ```
151
157
 
152
158
 
@@ -169,6 +175,16 @@ type IDecodedError = {
169
175
  message: string,
170
176
  code: IErrorCode,
171
177
  };
178
+
179
+ /**
180
+ * Exception Record
181
+ * The record type for the Exception class.
182
+ */
183
+ type IExceptionRecord = {
184
+ message: string;
185
+ code: IErrorCode;
186
+ data: unknown | null;
187
+ };
172
188
  ```
173
189
 
174
190
 
@@ -0,0 +1,23 @@
1
+ import { type IErrorCode } from '../shared/types.js';
2
+ import { IExceptionRecord } from './types.js';
3
+ export declare class Exception extends Error {
4
+ readonly code: IErrorCode;
5
+ readonly data: unknown;
6
+ constructor(error: unknown, code?: IErrorCode, data?: unknown);
7
+ /**
8
+ * Override the default toString method to return a formatted error message with the code.
9
+ * @returns A string representation of the error with the code.
10
+ */
11
+ toString(): string;
12
+ /**
13
+ * Override the default behavior for type conversion to return a formatted error message with the code.
14
+ * @param hint The type hint for the conversion.
15
+ * @returns A string representation of the error with the code or null for other types.
16
+ */
17
+ [Symbol.toPrimitive](hint: string): string | null;
18
+ /**
19
+ * Convert the exception to a record format.
20
+ * @returns An object representing the exception with message, code, and data.
21
+ */
22
+ toRecord(): IExceptionRecord;
23
+ }
@@ -0,0 +1 @@
1
+ import{decodeError,encodeError}from"../error-handler/index.js";export class Exception extends Error{code;data;constructor(e,r,t){const o=decodeError(e);super(o.message),this.name="Exception",this.code=r??o.code,this.data=t}toString(){return encodeError(this.message,this.code)}[Symbol.toPrimitive](e){return"string"===e||"default"===e?this.toString():null}toRecord(){return{message:this.message,code:this.code,data:this.data??null}}}
@@ -1,16 +1,2 @@
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
- }
1
+ export type { IExceptionRecord } from './types.js';
2
+ export { Exception } from './exception.js';
@@ -1 +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}}
1
+ export{Exception}from"./exception.js";
@@ -0,0 +1,6 @@
1
+ import { IErrorCode } from '../shared/types.js';
2
+ export type IExceptionRecord = {
3
+ message: string;
4
+ code: IErrorCode;
5
+ data: unknown | null;
6
+ };
@@ -0,0 +1 @@
1
+ export{};
package/dist/index.d.ts CHANGED
@@ -1,8 +1,4 @@
1
- import { IErrorCode, IDecodedError } from './shared/types.js';
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
- /**
6
- * Module exports
7
- */
8
- export { type IErrorCode, type IDecodedError, DEFAULT_MESSAGE, DEFAULT_CODE, extractMessage, encodeError, decodeError, isEncodedError, isDefaultErrorMessage, Exception, };
1
+ export { IErrorCode, IDecodedError } from './shared/types.js';
2
+ export { DEFAULT_CODE, DEFAULT_MESSAGE } from './shared/constants.js';
3
+ export { decodeError, encodeError, extractMessage, isDefaultErrorMessage, isEncodedError, } from './error-handler/index.js';
4
+ export { Exception } from './exception/exception.js';
package/dist/index.js CHANGED
@@ -1 +1 @@
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};
1
+ export{DEFAULT_CODE,DEFAULT_MESSAGE}from"./shared/constants.js";export{decodeError,encodeError,extractMessage,isDefaultErrorMessage,isEncodedError}from"./error-handler/index.js";export{Exception}from"./exception/exception.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "error-message-utils",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
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",