@@ -5,168 +5,134 @@ import { randomUUIDv7 } from 'bun';
55 */
66export interface BasaltErrorOptions < T = unknown > {
77 /**
8- * The error key .
8+ * The error message describing what went wrong .
99 */
10- key ?: readonly [ string , number ] | undefined ;
10+ message ?: string ;
11+
12+ /**
13+ * A unique key identifying the type of error, useful for localization or error handling.
14+ */
15+ key ?: string ;
16+
17+ /**
18+ * The HTTP status code associated with the error, typically used in API responses.
19+ */
20+ httpStatusCode ?: number ;
21+
1122 /**
12- * The cause of the error.
23+ * The cause of the error, which can be an original error or additional context .
1324 */
1425 cause ?: T ;
1526}
1627
1728/**
18- * Basalt error class that extends the ({@link Error}) class and provides additional properties. (uuidError, date, code, fileName, line, column)
29+ * A custom error class that extends the native {@link Error} class, providing additional properties
30+ * such as a unique identifier, error key, HTTP status code, and cause.
1931 *
20- * @typeParam T - The type of the cause of the error.
32+ * @typeParam T - The type of the cause of the error, which can be any object or error .
2133 *
2234 * @example
23- * The following example demonstrates how to throw a new instance of the Basalt error .
35+ * The following example demonstrates how to throw and catch a BasaltError .
2436 * ```typescript
2537 * try {
26- * throw new BasaltError();
38+ * throw new BasaltError({
39+ * message: 'An error occurred',
40+ * key: 'example.error',
41+ * httpStatusCode: 400,
42+ * cause: new Error('Original error')
43+ * });
2744 * } catch (error) {
28- * console.log(error instanceof BasaltError); // true
29- * console.log(error instanceof Error); // true
30- * // u can access to uuidError, date, code, fileName, line, column, message, name, stack, cause, toJSON
45+ * if (error instanceof BasaltError) {
46+ * console.error(`Error UUID: ${error.uuid}`);
47+ * console.error(`Error Date: ${error.date}`);
48+ * console.error(`Error Key: ${error.key}`);
49+ * console.error(`HTTP Status Code: ${error.httpStatusCode}`);
50+ * console.error(`Cause: ${error.cause}`);
51+ * }
3152 * }
3253 * ```
54+ *
3355 * @example
34- * The following example demonstrates how to create a new instance of the Basalt error with provided type for the cause.
56+ * The following example demonstrates how to create a BasaltError with a custom cause type .
3557 * ```typescript
36- * // { foo: 'bar' } is the type of the cause;
37- * const basaltError: BasaltError<{ foo: 'bar' }> = new BasaltError({
38- * key: 'error.unknown',
39- * cause: {
40- * foo: 'bar',
41- * },
58+ * const basaltError = new BasaltError<{ foo: string }>({
59+ * message: 'Custom error with cause',
60+ * key: 'basalt-package.error.custom_error',
61+ * httpStatusCode: 500,
62+ * cause: { foo: 'bar' },
4263 * });
4364 * console.log(basaltError.cause); // { foo: 'bar' }
4465 * ```
4566 */
4667export class BasaltError < const T = unknown > extends Error {
4768 /**
48- * The cause of the error. (if available)
69+ * The cause of the error, typically used to store the original error or additional context.
4970 */
5071 public override readonly cause : T | undefined ;
5172
5273 /**
53- * The unique identifier of the error.
54- * This identifier is used to track the error in the logs.
74+ * The unique identifier of the error, automatically generated using UUID v7 .
75+ * This identifier is particularly useful for tracking errors in logs.
5576 */
5677 private readonly _uuid : string = randomUUIDv7 ( ) ;
5778
5879 /**
59- * The date when the error was created.
80+ * The date when the error was created, automatically set to the current date and time .
6081 */
6182 private readonly _date : Date = new Date ( ) ;
6283
6384 /**
64- * The error code. (HTTP status code)
65- */
66- private readonly _statusCode : number ;
67-
68- /**
69- * The fileName where the error occurred (if available).
70- */
71- private readonly _fileName : string = '' ;
72-
73- /**
74- * The line number where the error occurred (if available).
85+ * A unique key identifying the type of error, useful for localization or error handling.
7586 */
76- private readonly _line : number = 0 ;
87+ private readonly _key : string ;
7788
7889 /**
79- * The column number where the error occurred (if available) .
90+ * The HTTP status code associated with the error, typically used in API responses .
8091 */
81- private readonly _column : number = 0 ;
92+ private readonly _httpStatusCode : number ;
8293
8394 /**
84- * Creates a new instance of the Basalt error .
95+ * Creates a new instance of the BasaltError class .
8596 *
8697 * @param basaltErrorOptions - The options for the Basalt error. ({@link BasaltErrorOptions})
8798 */
8899 public constructor ( basaltErrorOptions ?: Readonly < BasaltErrorOptions < T > > ) {
89- super ( basaltErrorOptions ?. key ?. [ 0 ] || 'error.unknown' ) ;
100+ super ( basaltErrorOptions ?. message ) ;
90101 super . name = 'BasaltError' ;
91102 this . cause = basaltErrorOptions ?. cause ;
92- this . _statusCode = basaltErrorOptions ?. key ?. [ 1 ] || 500 ;
93- const stackLine = this . stack ?. split ( '\n' ) [ 1 ] ?. trim ( ) ;
94- const match = stackLine ?. match ( / \( ? ( .+ ) : ( \d + ) : ( \d + ) \) ? $ / ) ;
95- if ( match ) {
96- this . _fileName = match [ 1 ] || '' ;
97- this . _line = parseInt ( match [ 2 ] , 10 ) || 0 ;
98- this . _column = parseInt ( match [ 3 ] , 10 ) || 0 ;
99- }
103+ this . _key = basaltErrorOptions ?. key || '' ;
104+ this . _httpStatusCode = basaltErrorOptions ?. httpStatusCode || 500 ;
100105 }
101106
102107 /**
103- * The unique identifier of the error.
108+ * Gets the unique identifier of the error.
109+ * @returns The UUID of the error.
104110 */
105111 public get uuid ( ) : string {
106112 return this . _uuid ;
107113 }
108114
109115 /**
110- * The date when the error was created.
116+ * Gets the date when the error was created.
117+ * @returns The creation date of the error.
111118 */
112119 public get date ( ) : Date {
113120 return this . _date ;
114121 }
115122
116123 /**
117- * The error code. (HTTP status code)
124+ * Gets the error key, which identifies the type of error.
125+ * @returns The key associated with the error.
118126 */
119- public get statusCode ( ) : number {
120- return this . _statusCode ;
127+ public get key ( ) : string {
128+ return this . _key ;
121129 }
122130
123131 /**
124- * The fileName where the error occurred (if available).
132+ * Gets the HTTP status code associated with the error.
133+ * @returns The HTTP status code.
125134 */
126- public get fileName ( ) : string {
127- return this . _fileName ;
135+ public get httpStatusCode ( ) : number {
136+ return this . _httpStatusCode ;
128137 }
129-
130- /**
131- * The line number where the error occurred (if available).
132- */
133- public get line ( ) : number {
134- return this . _line ;
135- }
136-
137- /**
138- * The column number where the error occurred (if available).
139- */
140- public get column ( ) : number {
141- return this . _column ;
142- }
143-
144- /**
145- * Converts the error object to a JSON object.
146- *
147- * @returns The error object as a JSON object.
148- */
149- public toJSON ( ) : {
150- name : string ;
151- uuid : string ;
152- date : Date ;
153- message : string ;
154- statusCode : number ;
155- cause : T | undefined ;
156- fileName : string ;
157- line : number ;
158- column : number ;
159- } {
160- return {
161- name : this . name ,
162- uuid : this . _uuid ,
163- date : this . _date ,
164- message : this . message ,
165- statusCode : this . _statusCode ,
166- cause : this . cause ,
167- fileName : this . _fileName ,
168- line : this . _line ,
169- column : this . _column
170- } ;
171- }
172- }
138+ }
0 commit comments