@alessiofrittoli/exception 3.0.0 → 3.1.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/README.md CHANGED
@@ -156,7 +156,7 @@ The constructor initializes an `AbortError` instance with a custom message and o
156
156
  | Parameter | Type | Description |
157
157
  |-----------|--------------------|-----------------------------------------------------------------|
158
158
  | `message` | `TMessage` | (Required) The error message. Can be a string or a custom type. |
159
- | `options` | `AbortErrorOptions`| (Optional) An object containing the error options. It extends the [`ExceptionOptions` Interface](#exceptionoptions-interface) and omit `code` and `name` properties wich are set by `AbortError` for you. |
159
+ | `options` | `AbortErrorOptions`| (Optional) An object containing the error options. It extends the [`ExceptionOptions` Interface](#exceptionoptions-interface) and omit the `name` property. The `code` defaults to `ErrorCode.ABORT` but can be optionally customized. |
160
160
 
161
161
  ---
162
162
 
@@ -194,6 +194,45 @@ try {
194
194
 
195
195
  ---
196
196
 
197
+ ##### Using custom Abort Error codes
198
+
199
+ ```ts
200
+ import { AbortError } from '@alessiofrittoli/exception/abort'
201
+
202
+ enum CustomAbortCode
203
+ {
204
+ CUSTOM_CODE = 'ERR:CUSTOM_ABORT_CODE',
205
+ }
206
+
207
+ try {
208
+ const controller = new AbortController()
209
+ const { signal } = controller
210
+
211
+ button.addEventListener( 'click', () => {
212
+ controller.abort(
213
+ new AbortError( 'User cancelled the request.', { code: CustomAbortCode.CUSTOM_CODE } )
214
+ )
215
+ } )
216
+
217
+ await fetch( ..., { signal } )
218
+ } catch ( error ) {
219
+
220
+ if ( AbortError.isException( error ) ) {
221
+ if ( AbortError.isAbortError( error, CustomAbortCode.CUSTOM_CODE ) ) {
222
+ // handle abort error
223
+ console.log( error.code ) // `error.code` is type of `CustomAbortCode.CUSTOM_CODE`.
224
+ return
225
+ }
226
+ // handle other Exception errors
227
+ return
228
+ }
229
+ // handle other unknown errors
230
+
231
+ }
232
+ ```
233
+
234
+ ---
235
+
197
236
  #### `ErrorCode` enum
198
237
 
199
238
  The `ErrorCode` enum is a utility that provides pre-defined constants for the most common error codes.\
package/dist/abort.d.mts CHANGED
@@ -6,7 +6,7 @@ import { ErrorCode } from './code.mjs';
6
6
  *
7
7
  * @extends ExceptionOptions
8
8
  */
9
- interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'> {
9
+ interface AbortErrorOptions<TCode = ErrorCode> extends Partial<Omit<ExceptionOptions<TCode>, 'name'>> {
10
10
  /** The HTTP status code associated with the Exception. */
11
11
  status?: number;
12
12
  }
@@ -17,8 +17,8 @@ interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'> {
17
17
  *
18
18
  * @extends Exception
19
19
  */
20
- declare class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT> implements AbortErrorOptions {
21
- constructor(reason: TMessage, options?: AbortErrorOptions);
20
+ declare class AbortError<TMessage = string, TCode = ErrorCode> extends Exception<TMessage, TCode> implements AbortErrorOptions<TCode> {
21
+ constructor(reason: TMessage, options?: AbortErrorOptions<TCode>);
22
22
  /**
23
23
  * Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.
24
24
  *
@@ -29,7 +29,7 @@ declare class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT>
29
29
  * @param error The error to check.
30
30
  * @returns `true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.
31
31
  */
32
- static isAbortError<TMessage = string>(error: any): error is AbortError<TMessage>;
32
+ static isAbortError<TMessage = string, TCode = ErrorCode>(error: any, code?: TCode): error is AbortError<TMessage, TCode>;
33
33
  }
34
34
 
35
35
  export { AbortError, type AbortErrorOptions };
package/dist/abort.d.ts CHANGED
@@ -6,7 +6,7 @@ import { ErrorCode } from './code.js';
6
6
  *
7
7
  * @extends ExceptionOptions
8
8
  */
9
- interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'> {
9
+ interface AbortErrorOptions<TCode = ErrorCode> extends Partial<Omit<ExceptionOptions<TCode>, 'name'>> {
10
10
  /** The HTTP status code associated with the Exception. */
11
11
  status?: number;
12
12
  }
@@ -17,8 +17,8 @@ interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'> {
17
17
  *
18
18
  * @extends Exception
19
19
  */
20
- declare class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT> implements AbortErrorOptions {
21
- constructor(reason: TMessage, options?: AbortErrorOptions);
20
+ declare class AbortError<TMessage = string, TCode = ErrorCode> extends Exception<TMessage, TCode> implements AbortErrorOptions<TCode> {
21
+ constructor(reason: TMessage, options?: AbortErrorOptions<TCode>);
22
22
  /**
23
23
  * Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.
24
24
  *
@@ -29,7 +29,7 @@ declare class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT>
29
29
  * @param error The error to check.
30
30
  * @returns `true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.
31
31
  */
32
- static isAbortError<TMessage = string>(error: any): error is AbortError<TMessage>;
32
+ static isAbortError<TMessage = string, TCode = ErrorCode>(error: any, code?: TCode): error is AbortError<TMessage, TCode>;
33
33
  }
34
34
 
35
35
  export { AbortError, type AbortErrorOptions };
package/dist/abort.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkOJYXDMWZjs = require('./chunk-OJYXDMWZ.js');require('./chunk-FXYDTIUG.js');var e=class extends _chunkOJYXDMWZjs.a{constructor(r,t){super(r,{name:"AbortError",code:"ERR:ABORT",...t})}static isAbortError(r){return super.isAbortError(r)}};exports.AbortError = e;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk4PCRWQETjs = require('./chunk-4PCRWQET.js');require('./chunk-FXYDTIUG.js');var t=class extends _chunk4PCRWQETjs.a{constructor(r,o={}){let s="AbortError",{code:n="ERR:ABORT",...i}=o;super(r,{...i,name:s,code:n})}static isAbortError(r,o){return super.isAbortError(r,o)}};exports.AbortError = t;
2
2
  //# sourceMappingURL=abort.js.map
package/dist/abort.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/alessio/Documents/Personal/projects/exception/dist/abort.js","../src/abort.ts"],"names":["AbortError","Exception","reason","options","error"],"mappings":"AAAA,+HAAuC,+BAA4B,ICsBtDA,CAAAA,CAAN,MAAA,QAAmCC,kBAC1C,CACC,WAAA,CAAaC,CAAAA,CAAkBC,CAAAA,CAC/B,CACC,KAAA,CAAOD,CAAAA,CAAQ,CAAE,IAAA,CAAM,YAAA,CAAc,IAAA,CAAA,WAAA,CAAuB,GAAGC,CAAQ,CAAE,CAC1E,CAcA,OAAO,YAAA,CAAiCC,CAAAA,CACxC,CACC,OAAO,KAAA,CAAM,YAAA,CAAcA,CAAM,CAClC,CACD,CAAA,CAAA,uBAAA","file":"/Users/alessio/Documents/Personal/projects/exception/dist/abort.js","sourcesContent":[null,"import { Exception, ExceptionOptions } from '.'\nimport { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @extends ExceptionOptions\n */\nexport interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'>\n{\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage The type of the message property of the Exception. Defaults to `string`.\n *\n * @extends Exception\n */\nexport class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT> implements AbortErrorOptions\n{\n\tconstructor( reason: TMessage, options?: AbortErrorOptions )\n\t{\n\t\tsuper( reason, { name: 'AbortError', code: ErrorCode.ABORT, ...options } )\n\t}\n\n\n\t/**\n\t * Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `AbortError<TMessage>`.\n\t *\n\t * @template TMessage The type of the message property of the AbortError.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isAbortError<TMessage = string>( error: any ): error is AbortError<TMessage>\n\t{\n\t\treturn super.isAbortError( error )\n\t}\n}"]}
1
+ {"version":3,"sources":["/Users/alessio/Documents/Personal/projects/exception/dist/abort.js","../src/abort.ts"],"names":["AbortError","Exception","reason","options","name","code","rest","error"],"mappings":"AAAA,+HAAuC,+BAA4B,ICsBtDA,CAAAA,CAAN,MAAA,QAEGC,kBACV,CACC,WAAA,CAAaC,CAAAA,CAAkBC,CAAAA,CAAoC,CAAC,CAAA,CACpE,CACC,IAAMC,CAAAA,CAAO,YAAA,CAEP,CACL,IAAA,CAAAC,CAAAA,CAAAA,WAAAA,CAAiC,GAAGC,CACrC,CAAA,CAAIH,CAAAA,CAEJ,KAAA,CAAOD,CAAAA,CAAQ,CAAE,GAAGI,CAAAA,CAAM,IAAA,CAAAF,CAAAA,CAAM,IAAA,CAAAC,CAAK,CAAE,CACxC,CAaA,OAAO,YAAA,CAGJE,CAAAA,CAAYF,CAAAA,CACf,CACC,OAAO,KAAA,CAAM,YAAA,CAAcE,CAAAA,CAAOF,CAAK,CACxC,CACD,CAAA,CAAA,uBAAA","file":"/Users/alessio/Documents/Personal/projects/exception/dist/abort.js","sourcesContent":[null,"import { Exception, type ExceptionOptions } from '.'\nimport { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @extends ExceptionOptions\n */\nexport interface AbortErrorOptions<TCode = ErrorCode> extends Partial<Omit<ExceptionOptions<TCode>, 'name'>>\n{\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage The type of the message property of the Exception. Defaults to `string`.\n *\n * @extends Exception\n */\nexport class AbortError<\n\tTMessage = string, TCode = ErrorCode\n> extends Exception<TMessage, TCode> implements AbortErrorOptions<TCode>\n{\n\tconstructor( reason: TMessage, options: AbortErrorOptions<TCode> = {} )\n\t{\n\t\tconst name = 'AbortError'\n\n\t\tconst {\n\t\t\tcode = ErrorCode.ABORT as TCode, ...rest\n\t\t} = options\n\n\t\tsuper( reason, { ...rest, name, code } )\n\t}\n\n\n\t/**\n\t * Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `AbortError<TMessage>`.\n\t *\n\t * @template TMessage The type of the message property of the AbortError.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\tstatic isAbortError<\n\t\tTMessage = string, TCode = ErrorCode\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t>( error: any, code?: TCode ): error is AbortError<TMessage, TCode>\n\t{\n\t\treturn super.isAbortError( error, code )\t\n\t}\n}"]}
package/dist/abort.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{a as o}from"./chunk-CLZRQMJF.mjs";import"./chunk-JM2G32JS.mjs";var e=class extends o{constructor(r,t){super(r,{name:"AbortError",code:"ERR:ABORT",...t})}static isAbortError(r){return super.isAbortError(r)}};export{e as AbortError};
1
+ import{a as e}from"./chunk-PPUBHX7P.mjs";import"./chunk-JM2G32JS.mjs";var t=class extends e{constructor(r,o={}){let s="AbortError",{code:n="ERR:ABORT",...i}=o;super(r,{...i,name:s,code:n})}static isAbortError(r,o){return super.isAbortError(r,o)}};export{t as AbortError};
2
2
  //# sourceMappingURL=abort.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/abort.ts"],"sourcesContent":["import { Exception, ExceptionOptions } from '.'\nimport { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @extends ExceptionOptions\n */\nexport interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'>\n{\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage The type of the message property of the Exception. Defaults to `string`.\n *\n * @extends Exception\n */\nexport class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT> implements AbortErrorOptions\n{\n\tconstructor( reason: TMessage, options?: AbortErrorOptions )\n\t{\n\t\tsuper( reason, { name: 'AbortError', code: ErrorCode.ABORT, ...options } )\n\t}\n\n\n\t/**\n\t * Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `AbortError<TMessage>`.\n\t *\n\t * @template TMessage The type of the message property of the AbortError.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isAbortError<TMessage = string>( error: any ): error is AbortError<TMessage>\n\t{\n\t\treturn super.isAbortError( error )\n\t}\n}"],"mappings":"sEAsBO,IAAMA,EAAN,cAAmCC,CAC1C,CACC,YAAaC,EAAkBC,EAC/B,CACC,MAAOD,EAAQ,CAAE,KAAM,aAAc,iBAAuB,GAAGC,CAAQ,CAAE,CAC1E,CAcA,OAAO,aAAiCC,EACxC,CACC,OAAO,MAAM,aAAcA,CAAM,CAClC,CACD","names":["AbortError","Exception","reason","options","error"]}
1
+ {"version":3,"sources":["../src/abort.ts"],"sourcesContent":["import { Exception, type ExceptionOptions } from '.'\nimport { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @extends ExceptionOptions\n */\nexport interface AbortErrorOptions<TCode = ErrorCode> extends Partial<Omit<ExceptionOptions<TCode>, 'name'>>\n{\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage The type of the message property of the Exception. Defaults to `string`.\n *\n * @extends Exception\n */\nexport class AbortError<\n\tTMessage = string, TCode = ErrorCode\n> extends Exception<TMessage, TCode> implements AbortErrorOptions<TCode>\n{\n\tconstructor( reason: TMessage, options: AbortErrorOptions<TCode> = {} )\n\t{\n\t\tconst name = 'AbortError'\n\n\t\tconst {\n\t\t\tcode = ErrorCode.ABORT as TCode, ...rest\n\t\t} = options\n\n\t\tsuper( reason, { ...rest, name, code } )\n\t}\n\n\n\t/**\n\t * Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `AbortError<TMessage>`.\n\t *\n\t * @template TMessage The type of the message property of the AbortError.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\tstatic isAbortError<\n\t\tTMessage = string, TCode = ErrorCode\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t>( error: any, code?: TCode ): error is AbortError<TMessage, TCode>\n\t{\n\t\treturn super.isAbortError( error, code )\t\n\t}\n}"],"mappings":"sEAsBO,IAAMA,EAAN,cAEGC,CACV,CACC,YAAaC,EAAkBC,EAAoC,CAAC,EACpE,CACC,IAAMC,EAAO,aAEP,CACL,KAAAC,cAAiC,GAAGC,CACrC,EAAIH,EAEJ,MAAOD,EAAQ,CAAE,GAAGI,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAE,CACxC,CAaA,OAAO,aAGJE,EAAYF,EACf,CACC,OAAO,MAAM,aAAcE,EAAOF,CAAK,CACxC,CACD","names":["AbortError","Exception","reason","options","name","code","rest","error"]}
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var o=class t extends Error{constructor(e,s){super("",s),this.__typename="Exception",this.name=s.name||this.__typename,this.message=e,this.code=s.code,this.status=s.status}static isException(e){return e instanceof t||typeof e=="object"&&"__typename"in e&&e.__typename==="Exception"}static isAbortError(e){return t.isException(e)&&(e.name==="AbortError"||e.code==="ERR:ABORT")}toJSON(){return{...this,message:this.message,cause:this.cause}}};exports.a = o;
2
- //# sourceMappingURL=chunk-OJYXDMWZ.js.map
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var o=class t extends Error{constructor(e,s){super("",s),this.__typename="Exception",this.name=s.name||this.__typename,this.message=e,this.code=s.code,this.status=s.status}static isException(e){return e instanceof t||typeof e=="object"&&"__typename"in e&&e.__typename==="Exception"}static isAbortError(e,s="ERR:ABORT"){return t.isException(e)&&(e.name==="AbortError"||e.code===s)}toJSON(){return{...this,message:this.message,cause:this.cause}}};exports.a = o;
2
+ //# sourceMappingURL=chunk-4PCRWQET.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["Exception","_Exception","message","options","error","code"],"mappings":"AA4BO,6EAAMA,CAAAA,CAAN,MAAMC,EAAAA,QAAwD,KACrE,CAeC,WAAA,CAAaC,CAAAA,CAAmBC,CAAAA,CAChC,CACC,KAAA,CAAO,EAAA,CAAIA,CAAQ,CAAA,CAEnB,IAAA,CAAK,UAAA,CAAa,WAAA,CAClB,IAAA,CAAK,IAAA,CAAQA,CAAAA,CAAQ,IAAA,EAAQ,IAAA,CAAK,UAAA,CAClC,IAAA,CAAK,OAAA,CAAUD,CAAAA,CACf,IAAA,CAAK,IAAA,CAAQC,CAAAA,CAAQ,IAAA,CACrB,IAAA,CAAK,MAAA,CAAUA,CAAAA,CAAQ,MACxB,CAcA,OAAO,WAAA,CAAmDC,CAAAA,CAC1D,CACC,OACCA,EAAAA,WAAiBH,CAAAA,EACf,OAAOG,CAAAA,EAAU,QAAA,EAAY,YAAA,GAAgBA,CAAAA,EAASA,CAAAA,CAAM,UAAA,GAAe,WAE/E,CAaA,OAAO,YAAA,CAENA,CAAAA,CAAYC,CAAAA,CAAAA,WAAAA,CAEb,CACC,OACCJ,CAAAA,CAAU,WAAA,CAAaG,CAAM,CAAA,EAAA,CAC3BA,CAAAA,CAAM,IAAA,GAAS,YAAA,EAAgBA,CAAAA,CAAM,IAAA,GAASC,CAAAA,CAElD,CAQA,MAAA,CAAA,CACA,CACC,MAAO,CACN,GAAG,IAAA,CACH,OAAA,CAAU,IAAA,CAAK,OAAA,CACf,KAAA,CAAQ,IAAA,CAAK,KACd,CACD,CACD,CAAA,CAAA,cAAA","file":"/Users/alessio/Documents/Personal/projects/exception/dist/chunk-4PCRWQET.js","sourcesContent":["import { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @template TCode The type of the error code.\n * @extends ErrorOptions\n */\nexport interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions\n{\n\t/** The error code associated with the Exception. */\n\tcode: TCode\n\t/** The name of the Exception. */\n\tname?: string\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage\tThe type of the message property of the Exception. Defaults to `string`.\n * @template TCode\t\tThe type of the code property of the Exception. Defaults to `ErrorCode`.\n *\n * @extends Error\n * @implements ExceptionOptions<TCode>\n */\nexport class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode>\n{\n\t// @ts-expect-error Type 'TMessage' is not assignable to type 'string'.ts(2416)\n\tmessage: TMessage\n\tstatus\n\tname\n\tprivate __typename: 'Exception'\n\tcode\n\n\n\t/**\n\t * Constructs a new Exception instance.\n\t *\n\t * @param message The message describing the exception.\n\t * @param options Additional options for the exception.\n\t */\n\tconstructor( message: TMessage, options: ExceptionOptions<TCode> ) \n\t{\n\t\tsuper( '', options )\n\n\t\tthis.__typename\t= 'Exception'\n\t\tthis.name\t\t= options.name || this.__typename\n\t\tthis.message\t= message\n\t\tthis.code\t\t= options.code\n\t\tthis.status\t\t= options.status\n\t}\n\n\t\n\t\n\t/**\n\t * Determines if the provided error is an instance of the Exception class.\n\t *\n\t * @template TMessage\tThe type of the message property of the Exception.\n\t * @template TCode\t\tThe type of the code property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an instance of Exception or has a `__typename` property equal to 'Exception', `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isException<TMessage = string, TCode = ErrorCode>( error: any ): error is Exception<TMessage, TCode>\n\t{\n\t\treturn (\n\t\t\terror instanceof Exception ||\n\t\t\t( typeof error === 'object' && '__typename' in error && error.__typename === 'Exception' )\n\t\t)\n\t}\n\t\n\n\t/**\n\t * Determines if the provided error is an instance of the Exception class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `Exception<TMessage, ErrorCode.ABORT>`.\n\t *\n\t * @template TMessage The type of the message property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\tstatic isAbortError<TMessage = string, TCode = ErrorCode>(\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\terror: any, code: TCode = ErrorCode.ABORT as TCode\n\t): error is Exception<TMessage, TCode>\n\t{\n\t\treturn (\n\t\t\tException.isException( error ) &&\n\t\t\t( error.name === 'AbortError' || error.code === code )\n\t\t)\n\t}\n\n\n\t/**\n\t * Converts the instance to a JSON object.\n\t *\n\t * @returns A JSON representation of the instance, including the message property.\n\t */\n\ttoJSON()\n\t{\n\t\treturn {\n\t\t\t...this,\n\t\t\tmessage\t: this.message,\n\t\t\tcause\t: this.cause,\n\t\t}\n\t}\n}"]}
@@ -1,2 +1,2 @@
1
- var o=class t extends Error{constructor(e,s){super("",s),this.__typename="Exception",this.name=s.name||this.__typename,this.message=e,this.code=s.code,this.status=s.status}static isException(e){return e instanceof t||typeof e=="object"&&"__typename"in e&&e.__typename==="Exception"}static isAbortError(e){return t.isException(e)&&(e.name==="AbortError"||e.code==="ERR:ABORT")}toJSON(){return{...this,message:this.message,cause:this.cause}}};export{o as a};
2
- //# sourceMappingURL=chunk-CLZRQMJF.mjs.map
1
+ var o=class t extends Error{constructor(e,s){super("",s),this.__typename="Exception",this.name=s.name||this.__typename,this.message=e,this.code=s.code,this.status=s.status}static isException(e){return e instanceof t||typeof e=="object"&&"__typename"in e&&e.__typename==="Exception"}static isAbortError(e,s="ERR:ABORT"){return t.isException(e)&&(e.name==="AbortError"||e.code===s)}toJSON(){return{...this,message:this.message,cause:this.cause}}};export{o as a};
2
+ //# sourceMappingURL=chunk-PPUBHX7P.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @template TCode The type of the error code.\n * @extends ErrorOptions\n */\nexport interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions\n{\n\t/** The error code associated with the Exception. */\n\tcode: TCode\n\t/** The name of the Exception. */\n\tname?: string\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage\tThe type of the message property of the Exception. Defaults to `string`.\n * @template TCode\t\tThe type of the code property of the Exception. Defaults to `ErrorCode`.\n *\n * @extends Error\n * @implements ExceptionOptions<TCode>\n */\nexport class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode>\n{\n\t// @ts-expect-error Type 'TMessage' is not assignable to type 'string'.ts(2416)\n\tmessage: TMessage\n\tstatus\n\tname\n\tprivate __typename: 'Exception'\n\tcode\n\n\n\t/**\n\t * Constructs a new Exception instance.\n\t *\n\t * @param message The message describing the exception.\n\t * @param options Additional options for the exception.\n\t */\n\tconstructor( message: TMessage, options: ExceptionOptions<TCode> ) \n\t{\n\t\tsuper( '', options )\n\n\t\tthis.__typename\t= 'Exception'\n\t\tthis.name\t\t= options.name || this.__typename\n\t\tthis.message\t= message\n\t\tthis.code\t\t= options.code\n\t\tthis.status\t\t= options.status\n\t}\n\n\t\n\t\n\t/**\n\t * Determines if the provided error is an instance of the Exception class.\n\t *\n\t * @template TMessage\tThe type of the message property of the Exception.\n\t * @template TCode\t\tThe type of the code property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an instance of Exception or has a `__typename` property equal to 'Exception', `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isException<TMessage = string, TCode = ErrorCode>( error: any ): error is Exception<TMessage, TCode>\n\t{\n\t\treturn (\n\t\t\terror instanceof Exception ||\n\t\t\t( typeof error === 'object' && '__typename' in error && error.__typename === 'Exception' )\n\t\t)\n\t}\n\t\n\n\t/**\n\t * Determines if the provided error is an instance of the Exception class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `Exception<TMessage, ErrorCode.ABORT>`.\n\t *\n\t * @template TMessage The type of the message property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isAbortError<TMessage = string>( error: any ): error is Exception<TMessage, ErrorCode.ABORT>\n\t{\n\t\treturn (\n\t\t\tException.isException( error ) &&\n\t\t\t( error.name === 'AbortError' || error.code === ErrorCode.ABORT )\n\t\t)\n\t}\n\n\n\t/**\n\t * Converts the instance to a JSON object.\n\t *\n\t * @returns A JSON representation of the instance, including the message property.\n\t */\n\ttoJSON()\n\t{\n\t\treturn {\n\t\t\t...this,\n\t\t\tmessage\t: this.message,\n\t\t\tcause\t: this.cause,\n\t\t}\n\t}\n}"],"mappings":"AA4BO,IAAMA,EAAN,MAAMC,UAAwD,KACrE,CAeC,YAAaC,EAAmBC,EAChC,CACC,MAAO,GAAIA,CAAQ,EAEnB,KAAK,WAAa,YAClB,KAAK,KAAQA,EAAQ,MAAQ,KAAK,WAClC,KAAK,QAAUD,EACf,KAAK,KAAQC,EAAQ,KACrB,KAAK,OAAUA,EAAQ,MACxB,CAcA,OAAO,YAAmDC,EAC1D,CACC,OACCA,aAAiBH,GACf,OAAOG,GAAU,UAAY,eAAgBA,GAASA,EAAM,aAAe,WAE/E,CAcA,OAAO,aAAiCA,EACxC,CACC,OACCH,EAAU,YAAaG,CAAM,IAC3BA,EAAM,OAAS,cAAgBA,EAAM,OAAS,YAElD,CAQA,QACA,CACC,MAAO,CACN,GAAG,KACH,QAAU,KAAK,QACf,MAAQ,KAAK,KACd,CACD,CACD","names":["Exception","_Exception","message","options","error"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @template TCode The type of the error code.\n * @extends ErrorOptions\n */\nexport interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions\n{\n\t/** The error code associated with the Exception. */\n\tcode: TCode\n\t/** The name of the Exception. */\n\tname?: string\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage\tThe type of the message property of the Exception. Defaults to `string`.\n * @template TCode\t\tThe type of the code property of the Exception. Defaults to `ErrorCode`.\n *\n * @extends Error\n * @implements ExceptionOptions<TCode>\n */\nexport class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode>\n{\n\t// @ts-expect-error Type 'TMessage' is not assignable to type 'string'.ts(2416)\n\tmessage: TMessage\n\tstatus\n\tname\n\tprivate __typename: 'Exception'\n\tcode\n\n\n\t/**\n\t * Constructs a new Exception instance.\n\t *\n\t * @param message The message describing the exception.\n\t * @param options Additional options for the exception.\n\t */\n\tconstructor( message: TMessage, options: ExceptionOptions<TCode> ) \n\t{\n\t\tsuper( '', options )\n\n\t\tthis.__typename\t= 'Exception'\n\t\tthis.name\t\t= options.name || this.__typename\n\t\tthis.message\t= message\n\t\tthis.code\t\t= options.code\n\t\tthis.status\t\t= options.status\n\t}\n\n\t\n\t\n\t/**\n\t * Determines if the provided error is an instance of the Exception class.\n\t *\n\t * @template TMessage\tThe type of the message property of the Exception.\n\t * @template TCode\t\tThe type of the code property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an instance of Exception or has a `__typename` property equal to 'Exception', `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isException<TMessage = string, TCode = ErrorCode>( error: any ): error is Exception<TMessage, TCode>\n\t{\n\t\treturn (\n\t\t\terror instanceof Exception ||\n\t\t\t( typeof error === 'object' && '__typename' in error && error.__typename === 'Exception' )\n\t\t)\n\t}\n\t\n\n\t/**\n\t * Determines if the provided error is an instance of the Exception class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `Exception<TMessage, ErrorCode.ABORT>`.\n\t *\n\t * @template TMessage The type of the message property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\tstatic isAbortError<TMessage = string, TCode = ErrorCode>(\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\terror: any, code: TCode = ErrorCode.ABORT as TCode\n\t): error is Exception<TMessage, TCode>\n\t{\n\t\treturn (\n\t\t\tException.isException( error ) &&\n\t\t\t( error.name === 'AbortError' || error.code === code )\n\t\t)\n\t}\n\n\n\t/**\n\t * Converts the instance to a JSON object.\n\t *\n\t * @returns A JSON representation of the instance, including the message property.\n\t */\n\ttoJSON()\n\t{\n\t\treturn {\n\t\t\t...this,\n\t\t\tmessage\t: this.message,\n\t\t\tcause\t: this.cause,\n\t\t}\n\t}\n}"],"mappings":"AA4BO,IAAMA,EAAN,MAAMC,UAAwD,KACrE,CAeC,YAAaC,EAAmBC,EAChC,CACC,MAAO,GAAIA,CAAQ,EAEnB,KAAK,WAAa,YAClB,KAAK,KAAQA,EAAQ,MAAQ,KAAK,WAClC,KAAK,QAAUD,EACf,KAAK,KAAQC,EAAQ,KACrB,KAAK,OAAUA,EAAQ,MACxB,CAcA,OAAO,YAAmDC,EAC1D,CACC,OACCA,aAAiBH,GACf,OAAOG,GAAU,UAAY,eAAgBA,GAASA,EAAM,aAAe,WAE/E,CAaA,OAAO,aAENA,EAAYC,cAEb,CACC,OACCJ,EAAU,YAAaG,CAAM,IAC3BA,EAAM,OAAS,cAAgBA,EAAM,OAASC,EAElD,CAQA,QACA,CACC,MAAO,CACN,GAAG,KACH,QAAU,KAAK,QACf,MAAQ,KAAK,KACd,CACD,CACD","names":["Exception","_Exception","message","options","error","code"]}
package/dist/index.d.mts CHANGED
@@ -56,7 +56,7 @@ declare class Exception<TMessage = string, TCode = ErrorCode> extends Error impl
56
56
  * @param error The error to check.
57
57
  * @returns `true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.
58
58
  */
59
- static isAbortError<TMessage = string>(error: any): error is Exception<TMessage, ErrorCode.ABORT>;
59
+ static isAbortError<TMessage = string, TCode = ErrorCode>(error: any, code?: TCode): error is Exception<TMessage, TCode>;
60
60
  /**
61
61
  * Converts the instance to a JSON object.
62
62
  *
package/dist/index.d.ts CHANGED
@@ -56,7 +56,7 @@ declare class Exception<TMessage = string, TCode = ErrorCode> extends Error impl
56
56
  * @param error The error to check.
57
57
  * @returns `true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.
58
58
  */
59
- static isAbortError<TMessage = string>(error: any): error is Exception<TMessage, ErrorCode.ABORT>;
59
+ static isAbortError<TMessage = string, TCode = ErrorCode>(error: any, code?: TCode): error is Exception<TMessage, TCode>;
60
60
  /**
61
61
  * Converts the instance to a JSON object.
62
62
  *
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkOJYXDMWZjs = require('./chunk-OJYXDMWZ.js');require('./chunk-FXYDTIUG.js');exports.Exception = _chunkOJYXDMWZjs.a;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk4PCRWQETjs = require('./chunk-4PCRWQET.js');require('./chunk-FXYDTIUG.js');exports.Exception = _chunk4PCRWQETjs.a;
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{a}from"./chunk-CLZRQMJF.mjs";import"./chunk-JM2G32JS.mjs";export{a as Exception};
1
+ import{a}from"./chunk-PPUBHX7P.mjs";import"./chunk-JM2G32JS.mjs";export{a as Exception};
2
2
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alessiofrittoli/exception",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Handle errors with ease",
5
5
  "author": {
6
6
  "name": "Alessio Frittoli",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["Exception","_Exception","message","options","error"],"mappings":"AA4BO,6EAAMA,CAAAA,CAAN,MAAMC,EAAAA,QAAwD,KACrE,CAeC,WAAA,CAAaC,CAAAA,CAAmBC,CAAAA,CAChC,CACC,KAAA,CAAO,EAAA,CAAIA,CAAQ,CAAA,CAEnB,IAAA,CAAK,UAAA,CAAa,WAAA,CAClB,IAAA,CAAK,IAAA,CAAQA,CAAAA,CAAQ,IAAA,EAAQ,IAAA,CAAK,UAAA,CAClC,IAAA,CAAK,OAAA,CAAUD,CAAAA,CACf,IAAA,CAAK,IAAA,CAAQC,CAAAA,CAAQ,IAAA,CACrB,IAAA,CAAK,MAAA,CAAUA,CAAAA,CAAQ,MACxB,CAcA,OAAO,WAAA,CAAmDC,CAAAA,CAC1D,CACC,OACCA,EAAAA,WAAiBH,CAAAA,EACf,OAAOG,CAAAA,EAAU,QAAA,EAAY,YAAA,GAAgBA,CAAAA,EAASA,CAAAA,CAAM,UAAA,GAAe,WAE/E,CAcA,OAAO,YAAA,CAAiCA,CAAAA,CACxC,CACC,OACCH,CAAAA,CAAU,WAAA,CAAaG,CAAM,CAAA,EAAA,CAC3BA,CAAAA,CAAM,IAAA,GAAS,YAAA,EAAgBA,CAAAA,CAAM,IAAA,GAAS,WAAA,CAElD,CAQA,MAAA,CAAA,CACA,CACC,MAAO,CACN,GAAG,IAAA,CACH,OAAA,CAAU,IAAA,CAAK,OAAA,CACf,KAAA,CAAQ,IAAA,CAAK,KACd,CACD,CACD,CAAA,CAAA,cAAA","file":"/Users/alessio/Documents/Personal/projects/exception/dist/chunk-OJYXDMWZ.js","sourcesContent":["import { ErrorCode } from './code'\n\n/**\n * Interface representing the options for an Exception.\n *\n * @template TCode The type of the error code.\n * @extends ErrorOptions\n */\nexport interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions\n{\n\t/** The error code associated with the Exception. */\n\tcode: TCode\n\t/** The name of the Exception. */\n\tname?: string\n\t/** The HTTP status code associated with the Exception. */\n\tstatus?: number\n}\n\n\n/**\n * Exception Class.\n * \n * @template TMessage\tThe type of the message property of the Exception. Defaults to `string`.\n * @template TCode\t\tThe type of the code property of the Exception. Defaults to `ErrorCode`.\n *\n * @extends Error\n * @implements ExceptionOptions<TCode>\n */\nexport class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode>\n{\n\t// @ts-expect-error Type 'TMessage' is not assignable to type 'string'.ts(2416)\n\tmessage: TMessage\n\tstatus\n\tname\n\tprivate __typename: 'Exception'\n\tcode\n\n\n\t/**\n\t * Constructs a new Exception instance.\n\t *\n\t * @param message The message describing the exception.\n\t * @param options Additional options for the exception.\n\t */\n\tconstructor( message: TMessage, options: ExceptionOptions<TCode> ) \n\t{\n\t\tsuper( '', options )\n\n\t\tthis.__typename\t= 'Exception'\n\t\tthis.name\t\t= options.name || this.__typename\n\t\tthis.message\t= message\n\t\tthis.code\t\t= options.code\n\t\tthis.status\t\t= options.status\n\t}\n\n\t\n\t\n\t/**\n\t * Determines if the provided error is an instance of the Exception class.\n\t *\n\t * @template TMessage\tThe type of the message property of the Exception.\n\t * @template TCode\t\tThe type of the code property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an instance of Exception or has a `__typename` property equal to 'Exception', `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isException<TMessage = string, TCode = ErrorCode>( error: any ): error is Exception<TMessage, TCode>\n\t{\n\t\treturn (\n\t\t\terror instanceof Exception ||\n\t\t\t( typeof error === 'object' && '__typename' in error && error.__typename === 'Exception' )\n\t\t)\n\t}\n\t\n\n\t/**\n\t * Determines if the provided error is an instance of the Exception class and has the ABORT ErrorCode.\n\t * \n\t * This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `Exception<TMessage, ErrorCode.ABORT>`.\n\t *\n\t * @template TMessage The type of the message property of the Exception.\n\t * \n\t * @param\terror The error to check.\n\t * @returns\t`true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tstatic isAbortError<TMessage = string>( error: any ): error is Exception<TMessage, ErrorCode.ABORT>\n\t{\n\t\treturn (\n\t\t\tException.isException( error ) &&\n\t\t\t( error.name === 'AbortError' || error.code === ErrorCode.ABORT )\n\t\t)\n\t}\n\n\n\t/**\n\t * Converts the instance to a JSON object.\n\t *\n\t * @returns A JSON representation of the instance, including the message property.\n\t */\n\ttoJSON()\n\t{\n\t\treturn {\n\t\t\t...this,\n\t\t\tmessage\t: this.message,\n\t\t\tcause\t: this.cause,\n\t\t}\n\t}\n}"]}