@alessiofrittoli/exception 2.1.0 → 2.3.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 +79 -73
- package/dist/code.js +2 -1
- package/dist/code.js.map +1 -0
- package/dist/code.mjs +2 -1
- package/dist/code.mjs.map +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -0
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# Exception 🚦
|
|
2
2
|
|
|
3
|
-
Version
|
|
3
|
+
[![NPM Latest Version][version-badge]][npm-url] [![Coverage Status][coverage-badge]][coverage-url] [![NPM Monthly Downloads][downloads-badge]][npm-url] [![Dependencies][deps-badge]][deps-url]
|
|
4
4
|
|
|
5
|
-
[
|
|
5
|
+
[version-badge]: https://img.shields.io/npm/v/%40alessiofrittoli%2Fexception
|
|
6
|
+
[npm-url]: https://npmjs.org/package/%40alessiofrittoli%2Fexception
|
|
7
|
+
[coverage-badge]: https://coveralls.io/repos/github/alessiofrittoli/exception/badge.svg
|
|
8
|
+
[coverage-url]: https://coveralls.io/github/alessiofrittoli/exception
|
|
9
|
+
[downloads-badge]: https://img.shields.io/npm/dm/%40alessiofrittoli%2Fexception.svg
|
|
10
|
+
[deps-badge]: https://img.shields.io/librariesio/release/npm/%40alessiofrittoli%2Fexception
|
|
11
|
+
[deps-url]: https://libraries.io/npm/%40alessiofrittoli%2Fexception
|
|
6
12
|
|
|
7
13
|
## Handle errors with ease
|
|
8
14
|
|
|
@@ -12,12 +18,12 @@ This documentation describes the `Exception` class, which provides a structured
|
|
|
12
18
|
|
|
13
19
|
- [Getting started](#getting-started)
|
|
14
20
|
- [API Reference](#api-reference)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
- [ExceptionOptions Interface](#exceptionoptions-interface)
|
|
22
|
+
- [Exception Class](#exception-class)
|
|
23
|
+
- [Usage Scenarios](#usage-scenarios)
|
|
18
24
|
- [Development](#development)
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
- [ESLint](#eslint)
|
|
26
|
+
- [Jest](#jest)
|
|
21
27
|
- [Contributing](#contributing)
|
|
22
28
|
- [Security](#security)
|
|
23
29
|
- [Credits](#made-with-)
|
|
@@ -81,12 +87,12 @@ The constructor initializes an `Exception` instance with a custom message and op
|
|
|
81
87
|
import { Exception } from '@alessiofrittoli/exception'
|
|
82
88
|
|
|
83
89
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
throw new Exception( 'Resource not found', {
|
|
91
|
+
code : 'ERR:NOTFOUND',
|
|
92
|
+
status : 404,
|
|
93
|
+
} )
|
|
88
94
|
} catch ( error ) {
|
|
89
|
-
|
|
95
|
+
console.error( error )
|
|
90
96
|
}
|
|
91
97
|
```
|
|
92
98
|
|
|
@@ -102,24 +108,24 @@ It supports also a JSON representation of the `Exception` class (commonly return
|
|
|
102
108
|
|
|
103
109
|
```ts
|
|
104
110
|
try {
|
|
105
|
-
|
|
111
|
+
throw new Exception( 'Something went wrong', { code: 'ERR:UNKNOWN' } )
|
|
106
112
|
} catch ( error ) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
if ( Exception.isException( error ) ) {
|
|
114
|
+
// we can safely access `Exception` properties
|
|
115
|
+
console.error( `Error [${ error.code }]: ${ error.message }` )
|
|
116
|
+
} else {
|
|
117
|
+
console.error( error )
|
|
118
|
+
}
|
|
113
119
|
}
|
|
114
120
|
```
|
|
115
121
|
|
|
116
122
|
```ts
|
|
117
123
|
/** Simulates JSON Exception returned by a server JSON Response. */
|
|
118
124
|
const error = (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
125
|
+
JSON.parse( JSON.stringify( new Exception( 'Exception with custom name.', {
|
|
126
|
+
code: 0,
|
|
127
|
+
name: 'AbortError',
|
|
128
|
+
} ) ) )
|
|
123
129
|
)
|
|
124
130
|
|
|
125
131
|
console.log( Exception.isException( error ) ) // Outputs: true
|
|
@@ -137,12 +143,12 @@ The `Exception` class is ideal for creating domain-specific errors with addition
|
|
|
137
143
|
|
|
138
144
|
```ts
|
|
139
145
|
try {
|
|
140
|
-
|
|
146
|
+
await fetch( ... )
|
|
141
147
|
} catch ( error ) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
148
|
+
// error thrown by the server: Exception( 'Wrong value.', { code: ErrorCode.WRONG_VALUE, status: 422 } )
|
|
149
|
+
if ( Exception.isException( error ) ) {
|
|
150
|
+
console.log( error.code ) // `error.code` is type of `ErrorCode`.
|
|
151
|
+
}
|
|
146
152
|
}
|
|
147
153
|
```
|
|
148
154
|
|
|
@@ -180,15 +186,15 @@ To fill this gap, you can "extend" the `ErrorCode` enum by doing so:
|
|
|
180
186
|
|
|
181
187
|
```ts
|
|
182
188
|
// myproject/src/error-code.ts
|
|
183
|
-
import {
|
|
189
|
+
import { ErrorCode as Exception } from '@alessiofrittoli/exception/code'
|
|
184
190
|
|
|
185
191
|
/** Your project custom `ErrorCode`. */
|
|
186
192
|
export enum MyProjectErrorCode
|
|
187
193
|
{
|
|
188
|
-
|
|
194
|
+
INVALID_SIGN = 'ERR:INVALIDSIGN',
|
|
189
195
|
}
|
|
190
196
|
|
|
191
|
-
const ErrorCode = {
|
|
197
|
+
const ErrorCode = { Exception, MyProjectErrorCode }
|
|
192
198
|
type ErrorCode = MergedEnumValue<typeof ErrorCode>
|
|
193
199
|
|
|
194
200
|
export default ErrorCode
|
|
@@ -211,8 +217,8 @@ import { Exception } from '@alessiofrittoli/exception'
|
|
|
211
217
|
import { ErrorCode } from '@alessiofrittoli/exception/code'
|
|
212
218
|
|
|
213
219
|
throw new Exception( 'Password is a required field to log you in.', {
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
code : ErrorCode.EMPTY_VALUE,
|
|
221
|
+
status : 422,
|
|
216
222
|
} )
|
|
217
223
|
```
|
|
218
224
|
|
|
@@ -223,8 +229,8 @@ import { Exception } from '@alessiofrittoli/exception'
|
|
|
223
229
|
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`
|
|
224
230
|
|
|
225
231
|
throw new Exception( 'Invalid signature.', {
|
|
226
|
-
|
|
227
|
-
|
|
232
|
+
code : ErrorCode.MyProjectErrorCode.INVALID_SIGN,
|
|
233
|
+
status : 403,
|
|
228
234
|
} )
|
|
229
235
|
```
|
|
230
236
|
|
|
@@ -234,18 +240,18 @@ throw new Exception( 'Invalid signature.', {
|
|
|
234
240
|
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`
|
|
235
241
|
|
|
236
242
|
try {
|
|
237
|
-
|
|
243
|
+
...
|
|
238
244
|
} catch ( error ) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
245
|
+
if ( Exception.isException<string, ErrorCode>( error ) ) {
|
|
246
|
+
switch ( error.code ) { // `error.code` is now type of `ErrorCode` (custom).
|
|
247
|
+
case ErrorCode.MyProjectErrorCode.INVALID_SIGN:
|
|
248
|
+
console.log( 'The signature is not valid.' )
|
|
249
|
+
break
|
|
250
|
+
case ErrorCode.Exception.UNKNOWN:
|
|
251
|
+
default:
|
|
252
|
+
console.log( 'Unexpected error occured.' )
|
|
253
|
+
}
|
|
254
|
+
}
|
|
249
255
|
}
|
|
250
256
|
```
|
|
251
257
|
|
|
@@ -334,30 +340,30 @@ If you believe you have found a security vulnerability, we encourage you to **_r
|
|
|
334
340
|
### Made with ☕
|
|
335
341
|
|
|
336
342
|
<table style='display:flex;gap:20px;'>
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
</table>
|
|
343
|
+
<tbody>
|
|
344
|
+
<tr>
|
|
345
|
+
<td>
|
|
346
|
+
<img alt="avatar" src='https://avatars.githubusercontent.com/u/35973186' style='width:60px;border-radius:50%;object-fit:contain;'>
|
|
347
|
+
</td>
|
|
348
|
+
<td>
|
|
349
|
+
<table style='display:flex;gap:2px;flex-direction:column;'>
|
|
350
|
+
<tbody>
|
|
351
|
+
<tr>
|
|
352
|
+
<td>
|
|
353
|
+
<a href='https://github.com/alessiofrittoli' target='_blank' rel='noopener'>Alessio Frittoli</a>
|
|
354
|
+
</td>
|
|
355
|
+
</tr>
|
|
356
|
+
<tr>
|
|
357
|
+
<td>
|
|
358
|
+
<small>
|
|
359
|
+
<a href='https://alessiofrittoli.it' target='_blank' rel='noopener'>https://alessiofrittoli.it</a> |
|
|
360
|
+
<a href='mailto:info@alessiofrittoli.it' target='_blank' rel='noopener'>info@alessiofrittoli.it</a>
|
|
361
|
+
</small>
|
|
362
|
+
</td>
|
|
363
|
+
</tr>
|
|
364
|
+
</tbody>
|
|
365
|
+
</table>
|
|
366
|
+
</td>
|
|
367
|
+
</tr>
|
|
368
|
+
</tbody>
|
|
369
|
+
</table>
|
package/dist/code.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
'use strict';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.ErrorCode=E
|
|
1
|
+
'use strict';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.ErrorCode=E;//# sourceMappingURL=code.js.map
|
|
2
|
+
//# sourceMappingURL=code.js.map
|
package/dist/code.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/code.ts"],"names":["ErrorCode"],"mappings":"aAIO,IAAKA,OAEXA,CAAA,CAAA,OAAA,CAAW,cACXA,CAAA,CAAA,KAAA,CAAS,YACTA,CAAA,CAAA,WAAA,CAAc,iBACdA,CAAA,CAAA,WAAA,CAAc,iBACdA,CAAA,CAAA,OAAA,CAAW,cACXA,CAAA,CAAA,SAAA,CAAY,eAPDA,CAAA,CAAA,EAAA,CAAA,EAAA,EAAA","file":"code.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}"]}
|
package/dist/code.mjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
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||{});export{E as ErrorCode}
|
|
1
|
+
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||{});export{E as ErrorCode};//# sourceMappingURL=code.mjs.map
|
|
2
|
+
//# sourceMappingURL=code.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/code.ts"],"names":["ErrorCode"],"mappings":"AAIO,IAAKA,OAEXA,CAAA,CAAA,OAAA,CAAW,cACXA,CAAA,CAAA,KAAA,CAAS,YACTA,CAAA,CAAA,WAAA,CAAc,iBACdA,CAAA,CAAA,WAAA,CAAc,iBACdA,CAAA,CAAA,OAAA,CAAW,cACXA,CAAA,CAAA,SAAA,CAAY,eAPDA,CAAA,CAAA,EAAA,CAAA,EAAA,EAAA","file":"code.mjs","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}"]}
|
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
'use strict';var t=class o 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 o||typeof e=="object"&&"typename"in e&&e.typename==="Exception"}toJSON(){return {...this,message:this.message,cause:this.cause}}};exports.Exception=t
|
|
1
|
+
'use strict';var t=class o 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 o||typeof e=="object"&&"typename"in e&&e.typename==="Exception"}toJSON(){return {...this,message:this.message,cause:this.cause}}};exports.Exception=t;//# sourceMappingURL=index.js.map
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["Exception","_Exception","message","options","error"],"mappings":"aA4BO,IAAMA,CAAN,CAAA,MAAMC,CAAwD,SAAA,KACrE,CAeC,WAAA,CAAaC,CAAmBC,CAAAA,CAAAA,CAChC,CACC,KAAA,CAAO,EAAIA,CAAAA,CAAQ,CAEnB,CAAA,IAAA,CAAK,QAAW,CAAA,WAAA,CAChB,IAAK,CAAA,IAAA,CAAQA,CAAQ,CAAA,IAAA,EAAQ,IAAK,CAAA,QAAA,CAClC,IAAK,CAAA,OAAA,CAAUD,CACf,CAAA,IAAA,CAAK,IAAQC,CAAAA,CAAAA,CAAQ,KACrB,IAAK,CAAA,MAAA,CAAUA,CAAQ,CAAA,OACxB,CAcA,OAAO,WAAmDC,CAAAA,CAAAA,CAC1D,CACC,OACCA,CAAiBH,YAAAA,CAAAA,EACf,OAAOG,CAAAA,EAAU,QAAY,EAAA,UAAA,GAAcA,GAASA,CAAM,CAAA,QAAA,GAAa,WAE3E,CAQA,MACA,EAAA,CACC,OAAO,CACN,GAAG,IAAA,CACH,OAAS,CAAA,IAAA,CAAK,OACd,CAAA,KAAA,CAAO,IAAK,CAAA,KACb,CACD,CACD","file":"index.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\ttypename: '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\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: this.message,\n\t\t\tcause: this.cause,\n\t\t}\n\t}\n}"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
var t=class o 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 o||typeof e=="object"&&"typename"in e&&e.typename==="Exception"}toJSON(){return {...this,message:this.message,cause:this.cause}}};export{t as Exception}
|
|
1
|
+
var t=class o 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 o||typeof e=="object"&&"typename"in e&&e.typename==="Exception"}toJSON(){return {...this,message:this.message,cause:this.cause}}};export{t as Exception};//# sourceMappingURL=index.mjs.map
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["Exception","_Exception","message","options","error"],"mappings":"AA4BO,IAAMA,CAAN,CAAA,MAAMC,CAAwD,SAAA,KACrE,CAeC,WAAA,CAAaC,CAAmBC,CAAAA,CAAAA,CAChC,CACC,KAAA,CAAO,EAAIA,CAAAA,CAAQ,CAEnB,CAAA,IAAA,CAAK,QAAW,CAAA,WAAA,CAChB,IAAK,CAAA,IAAA,CAAQA,CAAQ,CAAA,IAAA,EAAQ,IAAK,CAAA,QAAA,CAClC,IAAK,CAAA,OAAA,CAAUD,CACf,CAAA,IAAA,CAAK,IAAQC,CAAAA,CAAAA,CAAQ,KACrB,IAAK,CAAA,MAAA,CAAUA,CAAQ,CAAA,OACxB,CAcA,OAAO,WAAmDC,CAAAA,CAAAA,CAC1D,CACC,OACCA,CAAiBH,YAAAA,CAAAA,EACf,OAAOG,CAAAA,EAAU,QAAY,EAAA,UAAA,GAAcA,GAASA,CAAM,CAAA,QAAA,GAAa,WAE3E,CAQA,MACA,EAAA,CACC,OAAO,CACN,GAAG,IAAA,CACH,OAAS,CAAA,IAAA,CAAK,OACd,CAAA,KAAA,CAAO,IAAK,CAAA,KACb,CACD,CACD","file":"index.mjs","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\ttypename: '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\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: this.message,\n\t\t\tcause: this.cause,\n\t\t}\n\t}\n}"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/exception",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Handle errors with ease",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
],
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
32
33
|
"import": "./dist/index.mjs",
|
|
33
34
|
"require": "./dist/index.js"
|
|
34
35
|
},
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
"build:watch": "tsup --watch",
|
|
45
46
|
"lint": "eslint",
|
|
46
47
|
"release": "node scripts/publish.js --verbose --npm",
|
|
47
|
-
"test": "
|
|
48
|
+
"test": "jest",
|
|
48
49
|
"test:watch": "jest --watchAll --verbose",
|
|
49
50
|
"test:jsdom": "JSDOM=true pnpm test:watch",
|
|
50
51
|
"test:ci": "jest --ci --verbose",
|
|
@@ -57,18 +58,18 @@
|
|
|
57
58
|
"test:coverage:jsdom:serve": "JSDOM=true pnpm test:coverage:serve",
|
|
58
59
|
"test:jest": "pnpm test:watch jest.test.ts",
|
|
59
60
|
"test:jest:jsdom": "JSDOM=true pnpm test:jest",
|
|
60
|
-
"test:exception": "pnpm test exception.test.ts"
|
|
61
|
+
"test:exception": "pnpm test:watch exception.test.ts"
|
|
61
62
|
},
|
|
62
63
|
"devDependencies": {
|
|
63
64
|
"@alessiofrittoli/node-scripts": "^2.2.1",
|
|
64
|
-
"@eslint/js": "^9.
|
|
65
|
+
"@eslint/js": "^9.18.0",
|
|
65
66
|
"@jest/globals": "^29.7.0",
|
|
66
67
|
"@types/jest": "^29.5.14",
|
|
67
|
-
"@types/node": "^22.10.
|
|
68
|
+
"@types/node": "^22.10.7",
|
|
68
69
|
"concurrently": "^9.1.2",
|
|
69
70
|
"dotenv": "^16.4.7",
|
|
70
71
|
"esbuild": "0.24.2",
|
|
71
|
-
"eslint": "^9.
|
|
72
|
+
"eslint": "^9.18.0",
|
|
72
73
|
"globals": "^15.14.0",
|
|
73
74
|
"http-server": "^14.1.1",
|
|
74
75
|
"jest": "^29.7.0",
|
|
@@ -76,7 +77,7 @@
|
|
|
76
77
|
"ts-jest": "^29.2.5",
|
|
77
78
|
"ts-node": "^10.9.2",
|
|
78
79
|
"tsup": "^8.3.5",
|
|
79
|
-
"typescript": "^5.7.
|
|
80
|
-
"typescript-eslint": "^8.
|
|
80
|
+
"typescript": "^5.7.3",
|
|
81
|
+
"typescript-eslint": "^8.21.0"
|
|
81
82
|
}
|
|
82
83
|
}
|