@alessiofrittoli/exception 3.3.0 → 3.4.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
@@ -13,7 +13,6 @@
13
13
  [downloads-badge]: https://img.shields.io/npm/dm/%40alessiofrittoli%2Fexception.svg
14
14
  [deps-badge]: https://img.shields.io/librariesio/release/npm/%40alessiofrittoli%2Fexception
15
15
  [deps-url]: https://libraries.io/npm/%40alessiofrittoli%2Fexception
16
-
17
16
  [sponsor-badge]: https://img.shields.io/static/v1?label=Fund%20this%20package&message=%E2%9D%A4&logo=GitHub&color=%23DB61A2
18
17
  [sponsor-url]: https://github.com/sponsors/alessiofrittoli
19
18
 
@@ -71,7 +70,7 @@ The `ExceptionOptions` interface defines the optional parameters for the `Except
71
70
  ##### Properties
72
71
 
73
72
  | Property | Type | Default | Description |
74
- |----------|----------|-------------|------------------------------------------------------------------|
73
+ | -------- | -------- | ----------- | ---------------------------------------------------------------- |
75
74
  | `code` | `TCode` | - | (Required) A numeric or custom code representing the error type. |
76
75
  | `name` | `string` | `Exception` | (Optional) A string representing the error name. |
77
76
  | `status` | `number` | - | (Optional) An HTTP status code associated with the error. |
@@ -89,22 +88,22 @@ The constructor initializes an `Exception` instance with a custom message and op
89
88
  ###### Parameters
90
89
 
91
90
  | Parameter | Type | Description |
92
- |-----------|--------------------|-----------------------------------------------------------------|
91
+ | --------- | ------------------ | --------------------------------------------------------------- |
93
92
  | `message` | `TMessage` | (Required) The error message. Can be a string or a custom type. |
94
93
  | `options` | `ExceptionOptions` | (Required) An object containing the error options. |
95
94
 
96
95
  ###### Example
97
96
 
98
97
  ```ts
99
- import { Exception } from '@alessiofrittoli/exception'
98
+ import { Exception } from "@alessiofrittoli/exception";
100
99
 
101
100
  try {
102
- throw new Exception( 'Resource not found', {
103
- code : 'ERR:NOTFOUND',
104
- status : 404,
105
- } )
106
- } catch ( error ) {
107
- console.error( error )
101
+ throw new Exception("Resource not found", {
102
+ code: "ERR:NOTFOUND",
103
+ status: 404,
104
+ });
105
+ } catch (error) {
106
+ console.error(error);
108
107
  }
109
108
  ```
110
109
 
@@ -120,27 +119,29 @@ It supports also a JSON representation of the `Exception` class (commonly return
120
119
 
121
120
  ```ts
122
121
  try {
123
- throw new Exception( 'Something went wrong', { code: 'ERR:UNKNOWN' } )
124
- } catch ( error ) {
125
- if ( Exception.isException( error ) ) {
122
+ throw new Exception("Something went wrong", { code: "ERR:UNKNOWN" });
123
+ } catch (error) {
124
+ if (Exception.isException(error)) {
126
125
  // we can safely access `Exception` properties
127
- console.error( `Error [${ error.code }]: ${ error.message }` )
126
+ console.error(`Error [${error.code}]: ${error.message}`);
128
127
  } else {
129
- console.error( error )
128
+ console.error(error);
130
129
  }
131
130
  }
132
131
  ```
133
132
 
134
133
  ```ts
135
134
  /** Simulates JSON Exception returned by a server JSON Response. */
136
- const error = (
137
- JSON.parse( JSON.stringify( new Exception( 'Exception with custom name.', {
138
- code: 0,
139
- name: 'AbortError',
140
- } ) ) )
141
- )
142
-
143
- console.log( Exception.isException( error ) ) // Outputs: true
135
+ const error = JSON.parse(
136
+ JSON.stringify(
137
+ new Exception("Exception with custom name.", {
138
+ code: 0,
139
+ name: "AbortError",
140
+ })
141
+ )
142
+ );
143
+
144
+ console.log(Exception.isException(error)); // Outputs: true
144
145
  ```
145
146
 
146
147
  ---
@@ -155,10 +156,10 @@ The constructor initializes an `AbortError` instance with a custom message and o
155
156
 
156
157
  ###### Parameters
157
158
 
158
- | Parameter | Type | Description |
159
- |-----------|--------------------|-----------------------------------------------------------------|
160
- | `message` | `TMessage` | (Required) The error message. Can be a string or a custom type. |
161
- | `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. |
159
+ | Parameter | Type | Description |
160
+ | --------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
161
+ | `message` | `TMessage` | (Required) The error message. Can be a string or a custom type. |
162
+ | `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. |
162
163
 
163
164
  ---
164
165
 
@@ -190,7 +191,7 @@ try {
190
191
  return
191
192
  }
192
193
  // handle other unknown errors
193
-
194
+
194
195
  }
195
196
  ```
196
197
 
@@ -229,7 +230,7 @@ try {
229
230
  return
230
231
  }
231
232
  // handle other unknown errors
232
-
233
+
233
234
  }
234
235
  ```
235
236
 
@@ -244,18 +245,18 @@ It is designed to simplify error handling and improve feedback quality returned
244
245
 
245
246
  <summary>Constants Overview</summary>
246
247
 
247
- | Constant | Value | Description |
248
- |-----------------|--------------------|---------------------------------------------------------|
249
- | `UNKNOWN` | `ERR:UNKNOWN` | Returned when an unexpected error occured. |
250
- | `ABORT` | `ERR:ABORT` | Returned when user abort the request. |
251
- | `EMPTY_VALUE` | `ERR:EMPTYVALUE` | Returned when a required value is "falsy". |
252
- | `WRONG_VALUE` | `ERR:WRONGVALUE` | Return when a required value is not the expected value. |
253
- | `EXPIRED` | `ERR:EXPIRED` | Returned when a requested has been performed too late. |
254
- | `TOO_EARLY` | `ERR:TOOEARLY` | Returned when a request has been performed too early. |
255
- | `TOO_MANY` | `ERR:TOOMANY` | Could be return in combination with a 429 Too Many Requests Response Status. |
256
- | `QUOTA_REACHED` | `ERR:QUOTAREACHED` | Returned when a specific quota has been reached. |
257
- | `NOT_FOUND` | `ERR:NOTFOUND` | Returned when a resource cannot be found. |
258
- | `OFFLINE` | `ERR:INTERNETDISCONNECTED` | Could be used to throw errors when user is offline. |
248
+ | Constant | Value | Description |
249
+ | --------------- | -------------------------- | ---------------------------------------------------------------------------- |
250
+ | `UNKNOWN` | `ERR:UNKNOWN` | Returned when an unexpected error occured. |
251
+ | `ABORT` | `ERR:ABORT` | Returned when user abort the request. |
252
+ | `EMPTY_VALUE` | `ERR:EMPTYVALUE` | Returned when a required value is "falsy". |
253
+ | `WRONG_VALUE` | `ERR:WRONGVALUE` | Return when a required value is not the expected value. |
254
+ | `EXPIRED` | `ERR:EXPIRED` | Returned when a requested has been performed too late. |
255
+ | `TOO_EARLY` | `ERR:TOOEARLY` | Returned when a request has been performed too early. |
256
+ | `TOO_MANY` | `ERR:TOOMANY` | Could be return in combination with a 429 Too Many Requests Response Status. |
257
+ | `QUOTA_REACHED` | `ERR:QUOTAREACHED` | Returned when a specific quota has been reached. |
258
+ | `NOT_FOUND` | `ERR:NOTFOUND` | Returned when a resource cannot be found. |
259
+ | `OFFLINE` | `ERR:INTERNETDISCONNECTED` | Could be used to throw errors when user is offline. |
259
260
 
260
261
  </details>
261
262
 
@@ -271,22 +272,17 @@ To fill this gap, you can "extend" the `ErrorCode` enum by doing so:
271
272
 
272
273
  ```ts
273
274
  // myproject/src/error-code.ts
274
- import { ErrorCode as Exception } from '@alessiofrittoli/exception/code'
275
+ import { ErrorCode as Exception } from "@alessiofrittoli/exception/code";
275
276
 
276
277
  /** Your project custom `ErrorCode`. */
277
- export enum MyProjectErrorCode
278
- {
279
- INVALID_SIGN = 'ERR:INVALIDSIGN',
280
- }
281
-
282
- const ErrorCode = { Exception, MyProjectErrorCode }
283
- type ErrorCode = MergedEnumValue<typeof ErrorCode>
278
+ export const MyProjectErrorCode = {
279
+ INVALID_SIGN: "ERR:INVALIDSIGN",
280
+ } as const;
284
281
 
285
- export default ErrorCode
282
+ export const ErrorCode = { ...Exception, ...MyProjectErrorCode };
283
+ export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
286
284
  ```
287
285
 
288
- ⚠️ The Type `MergedEnumValue<T>` is globally delcared from `@alessiofrittoli/type-utils` so make sure to install it if needed.
289
-
290
286
  </details>
291
287
 
292
288
  ---
@@ -296,38 +292,38 @@ export default ErrorCode
296
292
  ##### Using the default `ErrorCode` to throw a new `Exception`
297
293
 
298
294
  ```ts
299
- import { Exception } from '@alessiofrittoli/exception'
300
- import { ErrorCode } from '@alessiofrittoli/exception/code'
295
+ import { Exception } from "@alessiofrittoli/exception";
296
+ import { ErrorCode } from "@alessiofrittoli/exception/code";
301
297
 
302
- throw new Exception( 'Password is a required field to log you in.', {
303
- code : ErrorCode.EMPTY_VALUE,
304
- status : 422,
305
- } )
298
+ throw new Exception("Password is a required field to log you in.", {
299
+ code: ErrorCode.EMPTY_VALUE,
300
+ status: 422,
301
+ });
306
302
  ```
307
303
 
308
304
  ##### Using custom `ErrorCode` to throw a new `Exception`
309
305
 
310
306
  ```ts
311
- import { Exception } from '@alessiofrittoli/exception'
312
- import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`
307
+ import { Exception } from "@alessiofrittoli/exception";
308
+ import { ErrorCode } from "@/error-code"; // previously created in `myproject/src/error-code.ts` (see - Extending the `enum` in your project)
313
309
 
314
- throw new Exception( 'Invalid signature.', {
315
- code : ErrorCode.MyProjectErrorCode.INVALID_SIGN,
316
- status : 403,
317
- } )
310
+ throw new Exception("Invalid signature.", {
311
+ code: ErrorCode.INVALID_SIGN,
312
+ status: 403,
313
+ });
318
314
  ```
319
315
 
320
316
  ##### Using `ErrorCode` to handle errors
321
317
 
322
318
  ```ts
323
- import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`
319
+ import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts` (see - Extending the `enum` in your project)
324
320
 
325
321
  try {
326
322
  ...
327
323
  } catch ( error ) {
328
324
  if ( Exception.isException<string, ErrorCode>( error ) ) {
329
325
  switch ( error.code ) { // `error.code` is now type of `ErrorCode` (custom).
330
- case ErrorCode.MyProjectErrorCode.INVALID_SIGN:
326
+ case ErrorCode.INVALID_SIGN:
331
327
  console.log( 'The signature is not valid.' )
332
328
  break
333
329
  case ErrorCode.Exception.UNKNOWN:
package/dist/abort.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkYQB23ERMjs = require('./chunk-YQB23ERM.js');require('./chunk-5TBITEFP.js');var t=class extends _chunkYQB23ERMjs.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;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkOBRRZX4Djs = require('./chunk-OBRRZX4D.js');var _chunkPSRQLNM6js = require('./chunk-PSRQLNM6.js');var s=class extends _chunkOBRRZX4Djs.a{constructor(r,o={}){let n="AbortError",{code:i=_chunkPSRQLNM6js.a.ABORT,...d}=o;super(r,{...d,name:n,code:i})}static isAbortError(r,o){return super.isAbortError(r,o)}};exports.AbortError = s;
package/dist/abort.mjs CHANGED
@@ -1 +1 @@
1
- import{a as e}from"./chunk-LFZW7VUK.mjs";import"./chunk-F4EU4T25.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};
1
+ import{a as t}from"./chunk-RAHIXT6K.mjs";import{a as e}from"./chunk-VBWDYSF3.mjs";var s=class extends t{constructor(r,o={}){let n="AbortError",{code:i=e.ABORT,...d}=o;super(r,{...d,name:n,code:i})}static isAbortError(r,o){return super.isAbortError(r,o)}};export{s as AbortError};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkPSRQLNM6js = require('./chunk-PSRQLNM6.js');var r=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=_chunkPSRQLNM6js.a.ABORT){return t.isException(e)&&(e.name==="AbortError"||e.code===s)}toJSON(){return{...this,message:this.message,cause:this.cause}}};exports.a = r;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var R={UNKNOWN:"ERR:UNKNOWN",ABORT:"ERR:ABORT",EMPTY_VALUE:"ERR:EMPTYVALUE",WRONG_VALUE:"ERR:WRONGVALUE",EXPIRED:"ERR:EXPIRED",TOO_EARLY:"ERR:TOOEARLY",TOO_MANY:"ERR:TOOMANY",QUOTA_REACHED:"ERR:QUOTAREACHED",NOT_FOUND:"ERR:NOTFOUND",OFFLINE:"ERR:INTERNETDISCONNECTED"};exports.a = R;
@@ -0,0 +1 @@
1
+ import{a as o}from"./chunk-VBWDYSF3.mjs";var r=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=o.ABORT){return t.isException(e)&&(e.name==="AbortError"||e.code===s)}toJSON(){return{...this,message:this.message,cause:this.cause}}};export{r as a};
@@ -0,0 +1 @@
1
+ var O={UNKNOWN:"ERR:UNKNOWN",ABORT:"ERR:ABORT",EMPTY_VALUE:"ERR:EMPTYVALUE",WRONG_VALUE:"ERR:WRONGVALUE",EXPIRED:"ERR:EXPIRED",TOO_EARLY:"ERR:TOOEARLY",TOO_MANY:"ERR:TOOMANY",QUOTA_REACHED:"ERR:QUOTAREACHED",NOT_FOUND:"ERR:NOTFOUND",OFFLINE:"ERR:INTERNETDISCONNECTED"};export{O as a};
package/dist/code.d.mts CHANGED
@@ -2,17 +2,18 @@
2
2
  * Most common error codes.
3
3
  *
4
4
  */
5
- declare enum ErrorCode {
6
- UNKNOWN = "ERR:UNKNOWN",
7
- ABORT = "ERR:ABORT",
8
- EMPTY_VALUE = "ERR:EMPTYVALUE",
9
- WRONG_VALUE = "ERR:WRONGVALUE",
10
- EXPIRED = "ERR:EXPIRED",
11
- TOO_EARLY = "ERR:TOOEARLY",
12
- TOO_MANY = "ERR:TOOMANY",
13
- QUOTA_REACHED = "ERR:QUOTAREACHED",
14
- NOT_FOUND = "ERR:NOTFOUND",
15
- OFFLINE = "ERR:INTERNETDISCONNECTED"
16
- }
5
+ declare const ErrorCode: {
6
+ readonly UNKNOWN: "ERR:UNKNOWN";
7
+ readonly ABORT: "ERR:ABORT";
8
+ readonly EMPTY_VALUE: "ERR:EMPTYVALUE";
9
+ readonly WRONG_VALUE: "ERR:WRONGVALUE";
10
+ readonly EXPIRED: "ERR:EXPIRED";
11
+ readonly TOO_EARLY: "ERR:TOOEARLY";
12
+ readonly TOO_MANY: "ERR:TOOMANY";
13
+ readonly QUOTA_REACHED: "ERR:QUOTAREACHED";
14
+ readonly NOT_FOUND: "ERR:NOTFOUND";
15
+ readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
16
+ };
17
+ type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];
17
18
 
18
19
  export { ErrorCode };
package/dist/code.d.ts CHANGED
@@ -2,17 +2,18 @@
2
2
  * Most common error codes.
3
3
  *
4
4
  */
5
- declare enum ErrorCode {
6
- UNKNOWN = "ERR:UNKNOWN",
7
- ABORT = "ERR:ABORT",
8
- EMPTY_VALUE = "ERR:EMPTYVALUE",
9
- WRONG_VALUE = "ERR:WRONGVALUE",
10
- EXPIRED = "ERR:EXPIRED",
11
- TOO_EARLY = "ERR:TOOEARLY",
12
- TOO_MANY = "ERR:TOOMANY",
13
- QUOTA_REACHED = "ERR:QUOTAREACHED",
14
- NOT_FOUND = "ERR:NOTFOUND",
15
- OFFLINE = "ERR:INTERNETDISCONNECTED"
16
- }
5
+ declare const ErrorCode: {
6
+ readonly UNKNOWN: "ERR:UNKNOWN";
7
+ readonly ABORT: "ERR:ABORT";
8
+ readonly EMPTY_VALUE: "ERR:EMPTYVALUE";
9
+ readonly WRONG_VALUE: "ERR:WRONGVALUE";
10
+ readonly EXPIRED: "ERR:EXPIRED";
11
+ readonly TOO_EARLY: "ERR:TOOEARLY";
12
+ readonly TOO_MANY: "ERR:TOOMANY";
13
+ readonly QUOTA_REACHED: "ERR:QUOTAREACHED";
14
+ readonly NOT_FOUND: "ERR:NOTFOUND";
15
+ readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
16
+ };
17
+ type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];
17
18
 
18
19
  export { ErrorCode };
package/dist/code.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunk5TBITEFPjs = require('./chunk-5TBITEFP.js');exports.ErrorCode = _chunk5TBITEFPjs.a;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkPSRQLNM6js = require('./chunk-PSRQLNM6.js');exports.ErrorCode = _chunkPSRQLNM6js.a;
package/dist/code.mjs CHANGED
@@ -1 +1 @@
1
- import{a}from"./chunk-F4EU4T25.mjs";export{a as ErrorCode};
1
+ import{a}from"./chunk-VBWDYSF3.mjs";export{a as ErrorCode};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkYQB23ERMjs = require('./chunk-YQB23ERM.js');require('./chunk-5TBITEFP.js');exports.Exception = _chunkYQB23ERMjs.a;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkOBRRZX4Djs = require('./chunk-OBRRZX4D.js');require('./chunk-PSRQLNM6.js');exports.Exception = _chunkOBRRZX4Djs.a;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{a}from"./chunk-LFZW7VUK.mjs";import"./chunk-F4EU4T25.mjs";export{a as Exception};
1
+ import{a}from"./chunk-RAHIXT6K.mjs";import"./chunk-VBWDYSF3.mjs";export{a as Exception};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alessiofrittoli/exception",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "Handle errors with ease",
5
5
  "author": {
6
6
  "name": "Alessio Frittoli",
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var O=(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.TOO_MANY="ERR:TOOMANY",R.QUOTA_REACHED="ERR:QUOTAREACHED",R.NOT_FOUND="ERR:NOTFOUND",R.OFFLINE="ERR:INTERNETDISCONNECTED",R))(O||{});exports.a = O;
@@ -1 +0,0 @@
1
- var O=(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.TOO_MANY="ERR:TOOMANY",R.QUOTA_REACHED="ERR:QUOTAREACHED",R.NOT_FOUND="ERR:NOTFOUND",R.OFFLINE="ERR:INTERNETDISCONNECTED",R))(O||{});export{O as a};
@@ -1 +0,0 @@
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};
@@ -1 +0,0 @@
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;