@alessiofrittoli/exception 2.4.0 → 2.6.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 +41 -23
- package/dist/abort.d.mts +35 -0
- package/dist/abort.d.ts +35 -0
- package/dist/abort.js +2 -0
- package/dist/abort.js.map +1 -0
- package/dist/abort.mjs +2 -0
- package/dist/abort.mjs.map +1 -0
- package/dist/chunk-CLZRQMJF.mjs +2 -0
- package/dist/chunk-CLZRQMJF.mjs.map +1 -0
- package/dist/chunk-FXYDTIUG.js +2 -0
- package/dist/chunk-FXYDTIUG.js.map +1 -0
- package/dist/chunk-JM2G32JS.mjs +2 -0
- package/dist/chunk-JM2G32JS.mjs.map +1 -0
- package/dist/chunk-OJYXDMWZ.js +2 -0
- package/dist/chunk-OJYXDMWZ.js.map +1 -0
- package/dist/code.js +1 -1
- package/dist/code.js.map +1 -1
- package/dist/code.mjs +1 -1
- package/dist/code.mjs.map +1 -1
- package/dist/index.d.mts +16 -5
- package/dist/index.d.ts +16 -5
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -23
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ This documentation describes the `Exception` class, which provides a structured
|
|
|
27
27
|
- [API Reference](#api-reference)
|
|
28
28
|
- [ExceptionOptions Interface](#exceptionoptions-interface)
|
|
29
29
|
- [Exception Class](#exception-class)
|
|
30
|
+
- [AbortError Class](#aborterror-class)
|
|
30
31
|
- [Usage Scenarios](#usage-scenarios)
|
|
31
32
|
- [Development](#development)
|
|
32
33
|
- [Install depenendencies](#install-depenendencies)
|
|
@@ -97,8 +98,8 @@ import { Exception } from '@alessiofrittoli/exception'
|
|
|
97
98
|
|
|
98
99
|
try {
|
|
99
100
|
throw new Exception( 'Resource not found', {
|
|
100
|
-
code
|
|
101
|
-
status
|
|
101
|
+
code : 'ERR:NOTFOUND',
|
|
102
|
+
status : 404,
|
|
102
103
|
} )
|
|
103
104
|
} catch ( error ) {
|
|
104
105
|
console.error( error )
|
|
@@ -142,22 +143,52 @@ console.log( Exception.isException( error ) ) // Outputs: true
|
|
|
142
143
|
|
|
143
144
|
---
|
|
144
145
|
|
|
145
|
-
####
|
|
146
|
+
#### `AbortError` Class
|
|
147
|
+
|
|
148
|
+
The `AbortError` class extends the [`Exception` Class](#exception-class) and offers a preset configuration for easier usage.
|
|
146
149
|
|
|
147
|
-
#####
|
|
150
|
+
##### Constructor
|
|
148
151
|
|
|
149
|
-
The
|
|
152
|
+
The constructor initializes an `AbortError` instance with a custom message and options.
|
|
150
153
|
|
|
151
|
-
######
|
|
154
|
+
###### Parameters
|
|
155
|
+
|
|
156
|
+
| Parameter | Type | Description |
|
|
157
|
+
|-----------|--------------------|-----------------------------------------------------------------|
|
|
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. |
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
#### Usage Scenarios
|
|
164
|
+
|
|
165
|
+
##### Precise Abort Errors Handling
|
|
152
166
|
|
|
153
167
|
```ts
|
|
168
|
+
import { AbortError } from '@alessiofrittoli/exception/abort'
|
|
169
|
+
|
|
154
170
|
try {
|
|
155
|
-
|
|
171
|
+
const controller = new AbortController()
|
|
172
|
+
const { signal } = controller
|
|
173
|
+
|
|
174
|
+
button.addEventListener( 'click', () => {
|
|
175
|
+
controller.abort( new AbortError( 'User cancelled the request.' ) )
|
|
176
|
+
} )
|
|
177
|
+
|
|
178
|
+
await fetch( ..., { signal } )
|
|
156
179
|
} catch ( error ) {
|
|
157
|
-
|
|
158
|
-
if (
|
|
159
|
-
|
|
180
|
+
|
|
181
|
+
if ( AbortError.isException( error ) ) {
|
|
182
|
+
if ( AbortError.isAbortError( error ) ) {
|
|
183
|
+
// handle abort error
|
|
184
|
+
console.log( error.code ) // `error.code` is type of `ErrorCode.ABORT`.
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
// handle other Exception errors
|
|
188
|
+
return
|
|
160
189
|
}
|
|
190
|
+
// handle other unknown errors
|
|
191
|
+
|
|
161
192
|
}
|
|
162
193
|
```
|
|
163
194
|
|
|
@@ -306,25 +337,12 @@ Run all the defined test suites by running the following:
|
|
|
306
337
|
# Run tests and watch file changes.
|
|
307
338
|
pnpm test:watch
|
|
308
339
|
|
|
309
|
-
# Run tests and watch file changes with jest-environment-jsdom.
|
|
310
|
-
pnpm test:jsdom
|
|
311
|
-
|
|
312
340
|
# Run tests in a CI environment.
|
|
313
341
|
pnpm test:ci
|
|
314
|
-
|
|
315
|
-
# Run tests in a CI environment with jest-environment-jsdom.
|
|
316
|
-
pnpm test:ci:jsdom
|
|
317
342
|
```
|
|
318
343
|
|
|
319
|
-
You can eventually run specific suits like so:
|
|
320
|
-
|
|
321
344
|
- See [`package.json`](./package.json) file scripts for more info.
|
|
322
345
|
|
|
323
|
-
```bash
|
|
324
|
-
pnpm test:jest
|
|
325
|
-
pnpm test:jest:jsdom
|
|
326
|
-
```
|
|
327
|
-
|
|
328
346
|
Run tests with coverage.
|
|
329
347
|
|
|
330
348
|
An HTTP server is then started to serve coverage files from `./coverage` folder.
|
package/dist/abort.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ExceptionOptions, Exception } from './index.mjs';
|
|
2
|
+
import { ErrorCode } from './code.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Interface representing the options for an Exception.
|
|
6
|
+
*
|
|
7
|
+
* @extends ExceptionOptions
|
|
8
|
+
*/
|
|
9
|
+
interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'> {
|
|
10
|
+
/** The HTTP status code associated with the Exception. */
|
|
11
|
+
status?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Exception Class.
|
|
15
|
+
*
|
|
16
|
+
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
17
|
+
*
|
|
18
|
+
* @extends Exception
|
|
19
|
+
*/
|
|
20
|
+
declare class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT> implements AbortErrorOptions {
|
|
21
|
+
constructor(reason: TMessage, options?: AbortErrorOptions);
|
|
22
|
+
/**
|
|
23
|
+
* Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.
|
|
24
|
+
*
|
|
25
|
+
* This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `AbortError<TMessage>`.
|
|
26
|
+
*
|
|
27
|
+
* @template TMessage The type of the message property of the AbortError.
|
|
28
|
+
*
|
|
29
|
+
* @param error The error to check.
|
|
30
|
+
* @returns `true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.
|
|
31
|
+
*/
|
|
32
|
+
static isAbortError<TMessage = string>(error: any): error is AbortError<TMessage>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { AbortError, type AbortErrorOptions };
|
package/dist/abort.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ExceptionOptions, Exception } from './index.js';
|
|
2
|
+
import { ErrorCode } from './code.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Interface representing the options for an Exception.
|
|
6
|
+
*
|
|
7
|
+
* @extends ExceptionOptions
|
|
8
|
+
*/
|
|
9
|
+
interface AbortErrorOptions extends Omit<ExceptionOptions, 'code' | 'name'> {
|
|
10
|
+
/** The HTTP status code associated with the Exception. */
|
|
11
|
+
status?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Exception Class.
|
|
15
|
+
*
|
|
16
|
+
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
17
|
+
*
|
|
18
|
+
* @extends Exception
|
|
19
|
+
*/
|
|
20
|
+
declare class AbortError<TMessage> extends Exception<TMessage, ErrorCode.ABORT> implements AbortErrorOptions {
|
|
21
|
+
constructor(reason: TMessage, options?: AbortErrorOptions);
|
|
22
|
+
/**
|
|
23
|
+
* Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.
|
|
24
|
+
*
|
|
25
|
+
* This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `AbortError<TMessage>`.
|
|
26
|
+
*
|
|
27
|
+
* @template TMessage The type of the message property of the AbortError.
|
|
28
|
+
*
|
|
29
|
+
* @param error The error to check.
|
|
30
|
+
* @returns `true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.
|
|
31
|
+
*/
|
|
32
|
+
static isAbortError<TMessage = string>(error: any): error is AbortError<TMessage>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { AbortError, type AbortErrorOptions };
|
package/dist/abort.js
ADDED
|
@@ -0,0 +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;
|
|
2
|
+
//# sourceMappingURL=abort.js.map
|
|
@@ -0,0 +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}"]}
|
package/dist/abort.mjs
ADDED
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +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
|
|
@@ -0,0 +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"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var E=(R=>(R.UNKNOWN="ERR:UNKNOWN",R.ABORT="ERR:ABORT",R.EMPTY_VALUE="ERR:EMPTYVALUE",R.WRONG_VALUE="ERR:WRONGVALUE",R.EXPIRED="ERR:EXPIRED",R.TOO_EARLY="ERR:TOOEARLY",R))(E||{});exports.a = E;
|
|
2
|
+
//# sourceMappingURL=chunk-FXYDTIUG.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/code.ts"],"names":["ErrorCode"],"mappings":"AAIO,6EAAKA,CAAAA,CAAAA,CAAAA,CAAAA,EAAAA,CAEXA,CAAAA,CAAA,OAAA,CAAW,aAAA,CACXA,CAAAA,CAAA,KAAA,CAAS,WAAA,CACTA,CAAAA,CAAA,WAAA,CAAc,gBAAA,CACdA,CAAAA,CAAA,WAAA,CAAc,gBAAA,CACdA,CAAAA,CAAA,OAAA,CAAW,aAAA,CACXA,CAAAA,CAAA,SAAA,CAAY,cAAA,CAPDA,CAAAA,CAAAA,CAAAA,CAAAA,CAAAA,EAAA,CAAA,CAAA,CAAA,CAAA,cAAA","file":"/Users/alessio/Documents/Personal/projects/exception/dist/chunk-FXYDTIUG.js","sourcesContent":["/**\n * Most common error codes.\n * \n */\nexport enum ErrorCode\n{\n\tUNKNOWN\t\t= 'ERR:UNKNOWN',\n\tABORT\t\t= 'ERR:ABORT',\n\tEMPTY_VALUE\t= 'ERR:EMPTYVALUE',\n\tWRONG_VALUE\t= 'ERR:WRONGVALUE',\n\tEXPIRED\t\t= 'ERR:EXPIRED',\n\tTOO_EARLY\t= 'ERR:TOOEARLY',\n}"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/code.ts"],"sourcesContent":["/**\n * Most common error codes.\n * \n */\nexport enum ErrorCode\n{\n\tUNKNOWN\t\t= 'ERR:UNKNOWN',\n\tABORT\t\t= 'ERR:ABORT',\n\tEMPTY_VALUE\t= 'ERR:EMPTYVALUE',\n\tWRONG_VALUE\t= 'ERR:WRONGVALUE',\n\tEXPIRED\t\t= 'ERR:EXPIRED',\n\tTOO_EARLY\t= 'ERR:TOOEARLY',\n}"],"mappings":"AAIO,IAAKA,OAEXA,EAAA,QAAW,cACXA,EAAA,MAAS,YACTA,EAAA,YAAc,iBACdA,EAAA,YAAc,iBACdA,EAAA,QAAW,cACXA,EAAA,UAAY,eAPDA,OAAA","names":["ErrorCode"]}
|
|
@@ -0,0 +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
|
|
@@ -0,0 +1 @@
|
|
|
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}"]}
|
package/dist/code.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkFXYDTIUGjs = require('./chunk-FXYDTIUG.js');exports.ErrorCode = _chunkFXYDTIUGjs.a;
|
|
2
2
|
//# sourceMappingURL=code.js.map
|
package/dist/code.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"sources":["/Users/alessio/Documents/Personal/projects/exception/dist/code.js"],"names":[],"mappings":"AAAA,+HAAkC,uCAAuB","file":"/Users/alessio/Documents/Personal/projects/exception/dist/code.js"}
|
package/dist/code.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import{a}from"./chunk-JM2G32JS.mjs";export{a as ErrorCode};
|
|
2
2
|
//# sourceMappingURL=code.mjs.map
|
package/dist/code.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { ErrorCode } from './code.mjs';
|
|
|
3
3
|
/**
|
|
4
4
|
* Interface representing the options for an Exception.
|
|
5
5
|
*
|
|
6
|
-
* @template TCode
|
|
6
|
+
* @template TCode The type of the error code.
|
|
7
7
|
* @extends ErrorOptions
|
|
8
8
|
*/
|
|
9
9
|
interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions {
|
|
@@ -20,14 +20,14 @@ interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions {
|
|
|
20
20
|
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
21
21
|
* @template TCode The type of the code property of the Exception. Defaults to `ErrorCode`.
|
|
22
22
|
*
|
|
23
|
-
* @extends
|
|
24
|
-
* @implements
|
|
23
|
+
* @extends Error
|
|
24
|
+
* @implements ExceptionOptions<TCode>
|
|
25
25
|
*/
|
|
26
26
|
declare class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode> {
|
|
27
27
|
message: TMessage;
|
|
28
28
|
status: number | undefined;
|
|
29
29
|
name: string;
|
|
30
|
-
|
|
30
|
+
private __typename;
|
|
31
31
|
code: TCode;
|
|
32
32
|
/**
|
|
33
33
|
* Constructs a new Exception instance.
|
|
@@ -43,9 +43,20 @@ declare class Exception<TMessage = string, TCode = ErrorCode> extends Error impl
|
|
|
43
43
|
* @template TCode The type of the code property of the Exception.
|
|
44
44
|
*
|
|
45
45
|
* @param error The error to check.
|
|
46
|
-
* @returns `true` if the error is an instance of Exception or has a `
|
|
46
|
+
* @returns `true` if the error is an instance of Exception or has a `__typename` property equal to 'Exception', `false` otherwise.
|
|
47
47
|
*/
|
|
48
48
|
static isException<TMessage = string, TCode = ErrorCode>(error: any): error is Exception<TMessage, TCode>;
|
|
49
|
+
/**
|
|
50
|
+
* Determines if the provided error is an instance of the Exception class and has the ABORT ErrorCode.
|
|
51
|
+
*
|
|
52
|
+
* This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `Exception<TMessage, ErrorCode.ABORT>`.
|
|
53
|
+
*
|
|
54
|
+
* @template TMessage The type of the message property of the Exception.
|
|
55
|
+
*
|
|
56
|
+
* @param error The error to check.
|
|
57
|
+
* @returns `true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.
|
|
58
|
+
*/
|
|
59
|
+
static isAbortError<TMessage = string>(error: any): error is Exception<TMessage, ErrorCode.ABORT>;
|
|
49
60
|
/**
|
|
50
61
|
* Converts the instance to a JSON object.
|
|
51
62
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ErrorCode } from './code.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* Interface representing the options for an Exception.
|
|
5
5
|
*
|
|
6
|
-
* @template TCode
|
|
6
|
+
* @template TCode The type of the error code.
|
|
7
7
|
* @extends ErrorOptions
|
|
8
8
|
*/
|
|
9
9
|
interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions {
|
|
@@ -20,14 +20,14 @@ interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions {
|
|
|
20
20
|
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
21
21
|
* @template TCode The type of the code property of the Exception. Defaults to `ErrorCode`.
|
|
22
22
|
*
|
|
23
|
-
* @extends
|
|
24
|
-
* @implements
|
|
23
|
+
* @extends Error
|
|
24
|
+
* @implements ExceptionOptions<TCode>
|
|
25
25
|
*/
|
|
26
26
|
declare class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode> {
|
|
27
27
|
message: TMessage;
|
|
28
28
|
status: number | undefined;
|
|
29
29
|
name: string;
|
|
30
|
-
|
|
30
|
+
private __typename;
|
|
31
31
|
code: TCode;
|
|
32
32
|
/**
|
|
33
33
|
* Constructs a new Exception instance.
|
|
@@ -43,9 +43,20 @@ declare class Exception<TMessage = string, TCode = ErrorCode> extends Error impl
|
|
|
43
43
|
* @template TCode The type of the code property of the Exception.
|
|
44
44
|
*
|
|
45
45
|
* @param error The error to check.
|
|
46
|
-
* @returns `true` if the error is an instance of Exception or has a `
|
|
46
|
+
* @returns `true` if the error is an instance of Exception or has a `__typename` property equal to 'Exception', `false` otherwise.
|
|
47
47
|
*/
|
|
48
48
|
static isException<TMessage = string, TCode = ErrorCode>(error: any): error is Exception<TMessage, TCode>;
|
|
49
|
+
/**
|
|
50
|
+
* Determines if the provided error is an instance of the Exception class and has the ABORT ErrorCode.
|
|
51
|
+
*
|
|
52
|
+
* This won't work for `new DOMException( 'Abort reason', 'AbortError' )` since we type guard the checked value to `Exception<TMessage, ErrorCode.ABORT>`.
|
|
53
|
+
*
|
|
54
|
+
* @template TMessage The type of the message property of the Exception.
|
|
55
|
+
*
|
|
56
|
+
* @param error The error to check.
|
|
57
|
+
* @returns `true` if the error is an `Exception` and has the ABORT ErrorCode, `false` otherwise.
|
|
58
|
+
*/
|
|
59
|
+
static isAbortError<TMessage = string>(error: any): error is Exception<TMessage, ErrorCode.ABORT>;
|
|
49
60
|
/**
|
|
50
61
|
* Converts the instance to a JSON object.
|
|
51
62
|
*
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkOJYXDMWZjs = require('./chunk-OJYXDMWZ.js');require('./chunk-FXYDTIUG.js');exports.Exception = _chunkOJYXDMWZjs.a;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"sources":["/Users/alessio/Documents/Personal/projects/exception/dist/index.js"],"names":[],"mappings":"AAAA,+HAAkC,+BAA4B,uCAAuB","file":"/Users/alessio/Documents/Personal/projects/exception/dist/index.js"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import{a}from"./chunk-CLZRQMJF.mjs";import"./chunk-JM2G32JS.mjs";export{a as Exception};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/exception",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Handle errors with ease",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -43,6 +43,11 @@
|
|
|
43
43
|
"types": "./dist/code.d.ts",
|
|
44
44
|
"import": "./dist/code.mjs",
|
|
45
45
|
"require": "./dist/code.js"
|
|
46
|
+
},
|
|
47
|
+
"./abort": {
|
|
48
|
+
"types": "./dist/abort.d.ts",
|
|
49
|
+
"import": "./dist/abort.mjs",
|
|
50
|
+
"require": "./dist/abort.js"
|
|
46
51
|
}
|
|
47
52
|
},
|
|
48
53
|
"sideEffects": false,
|
|
@@ -59,44 +64,33 @@
|
|
|
59
64
|
"//2c": "*********************************************************************",
|
|
60
65
|
"test": "jest --verbose",
|
|
61
66
|
"test:watch": "jest --watchAll --verbose",
|
|
62
|
-
"test:jsdom": "JSDOM=true pnpm test:watch",
|
|
63
67
|
"test:ci": "jest --ci --verbose",
|
|
64
|
-
"test:ci:jsdom": "JSDOM=true pnpm test:ci",
|
|
65
68
|
"//3a": "*********************************************************************",
|
|
66
69
|
"//3b": "************************ UNIT TESTS COVERAGE ************************",
|
|
67
70
|
"//3c": "*********************************************************************",
|
|
68
71
|
"test:coverage": "pnpm test:watch --coverage",
|
|
69
|
-
"test:ci
|
|
72
|
+
"test:coverage:ci": "pnpm test:ci --coverage",
|
|
70
73
|
"test:serve-coverage": "http-server ./coverage/lcov-report --gzip true -p 0 -o --silent",
|
|
71
|
-
"test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\""
|
|
72
|
-
"test:coverage:jsdom": "JSDOM=true pnpm test:coverage",
|
|
73
|
-
"test:coverage:jsdom:serve": "JSDOM=true pnpm test:coverage:serve",
|
|
74
|
-
"//4a": "*********************************************************************",
|
|
75
|
-
"//4b": "************************ TARGETED UNIT TESTS ************************",
|
|
76
|
-
"//4c": "*********************************************************************",
|
|
77
|
-
"test:jest": "pnpm test:watch jest.test.ts",
|
|
78
|
-
"test:jest:jsdom": "JSDOM=true pnpm test:jest",
|
|
79
|
-
"test:exception": "pnpm test:watch exception.test.ts"
|
|
74
|
+
"test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\""
|
|
80
75
|
},
|
|
81
76
|
"devDependencies": {
|
|
82
|
-
"@alessiofrittoli/node-scripts": "^2.
|
|
83
|
-
"@eslint/js": "^9.
|
|
77
|
+
"@alessiofrittoli/node-scripts": "^2.4.0",
|
|
78
|
+
"@eslint/js": "^9.25.1",
|
|
84
79
|
"@jest/globals": "^29.7.0",
|
|
85
80
|
"@types/jest": "^29.5.14",
|
|
86
|
-
"@types/node": "^22.
|
|
81
|
+
"@types/node": "^22.15.2",
|
|
87
82
|
"concurrently": "^9.1.2",
|
|
88
|
-
"dotenv": "^16.
|
|
89
|
-
"esbuild": "0.25.
|
|
90
|
-
"eslint": "^9.
|
|
83
|
+
"dotenv": "^16.5.0",
|
|
84
|
+
"esbuild": "0.25.3",
|
|
85
|
+
"eslint": "^9.25.1",
|
|
91
86
|
"globals": "^16.0.0",
|
|
92
87
|
"http-server": "^14.1.1",
|
|
93
88
|
"jest": "^29.7.0",
|
|
94
|
-
"jest
|
|
95
|
-
"ts-jest": "^29.2.6",
|
|
89
|
+
"ts-jest": "^29.3.2",
|
|
96
90
|
"ts-node": "^10.9.2",
|
|
97
91
|
"tsup": "^8.4.0",
|
|
98
|
-
"typescript": "^5.8.
|
|
99
|
-
"typescript-eslint": "^8.
|
|
92
|
+
"typescript": "^5.8.3",
|
|
93
|
+
"typescript-eslint": "^8.31.0"
|
|
100
94
|
},
|
|
101
95
|
"pnpm": {
|
|
102
96
|
"onlyBuiltDependencies": [
|