@alessiofrittoli/exception 1.2.0 → 2.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 +10 -8
- package/dist/code.d.mts +1 -1
- package/dist/code.d.ts +1 -1
- package/dist/code.js +1 -1
- package/dist/code.mjs +1 -1
- package/dist/index.d.mts +44 -2
- package/dist/index.d.ts +44 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +15 -8
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Exception 🚦
|
|
2
2
|
|
|
3
|
-
Version 1.
|
|
3
|
+
Version 2.1.0
|
|
4
|
+
|
|
5
|
+
[](https://coveralls.io/github/alessiofrittoli/exception) [](https://libraries.io/npm/%40alessiofrittoli%2Fexception)
|
|
4
6
|
|
|
5
7
|
## Handle errors with ease
|
|
6
8
|
|
|
@@ -76,7 +78,7 @@ The constructor initializes an `Exception` instance with a custom message and op
|
|
|
76
78
|
###### Example
|
|
77
79
|
|
|
78
80
|
```ts
|
|
79
|
-
import Exception from '@alessiofrittoli/exception'
|
|
81
|
+
import { Exception } from '@alessiofrittoli/exception'
|
|
80
82
|
|
|
81
83
|
try {
|
|
82
84
|
throw new Exception( 'Resource not found', {
|
|
@@ -178,7 +180,7 @@ To fill this gap, you can "extend" the `ErrorCode` enum by doing so:
|
|
|
178
180
|
|
|
179
181
|
```ts
|
|
180
182
|
// myproject/src/error-code.ts
|
|
181
|
-
import ExceptionCode from '@alessiofrittoli/exception/code'
|
|
183
|
+
import { ExceptionCode } from '@alessiofrittoli/exception/code'
|
|
182
184
|
|
|
183
185
|
/** Your project custom `ErrorCode`. */
|
|
184
186
|
export enum MyProjectErrorCode
|
|
@@ -205,8 +207,8 @@ export default ErrorCode
|
|
|
205
207
|
#### Using the default `ErrorCode` to throw a new `Exception`
|
|
206
208
|
|
|
207
209
|
```ts
|
|
208
|
-
import Exception from '@alessiofrittoli/exception'
|
|
209
|
-
import ErrorCode from '@alessiofrittoli/exception/code'
|
|
210
|
+
import { Exception } from '@alessiofrittoli/exception'
|
|
211
|
+
import { ErrorCode } from '@alessiofrittoli/exception/code'
|
|
210
212
|
|
|
211
213
|
throw new Exception( 'Password is a required field to log you in.', {
|
|
212
214
|
code : ErrorCode.EMPTY_VALUE,
|
|
@@ -217,8 +219,8 @@ throw new Exception( 'Password is a required field to log you in.', {
|
|
|
217
219
|
#### Using custom `ErrorCode` to throw a new `Exception`
|
|
218
220
|
|
|
219
221
|
```ts
|
|
220
|
-
import Exception from '@alessiofrittoli/exception'
|
|
221
|
-
import ErrorCode from '@/error-code' // previously created in `myproject/src/error-code.ts`
|
|
222
|
+
import { Exception } from '@alessiofrittoli/exception'
|
|
223
|
+
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`
|
|
222
224
|
|
|
223
225
|
throw new Exception( 'Invalid signature.', {
|
|
224
226
|
code : ErrorCode.MyProjectErrorCode.INVALID_SIGN,
|
|
@@ -229,7 +231,7 @@ throw new Exception( 'Invalid signature.', {
|
|
|
229
231
|
#### Using `ErrorCode` to handle errors
|
|
230
232
|
|
|
231
233
|
```ts
|
|
232
|
-
import ErrorCode from '@/error-code' // previously created in `myproject/src/error-code.ts`
|
|
234
|
+
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`
|
|
233
235
|
|
|
234
236
|
try {
|
|
235
237
|
...
|
package/dist/code.d.mts
CHANGED
package/dist/code.d.ts
CHANGED
package/dist/code.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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||{})
|
|
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;
|
package/dist/code.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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||{})
|
|
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};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,60 @@
|
|
|
1
|
-
import ErrorCode from './code.mjs';
|
|
1
|
+
import { ErrorCode } from './code.mjs';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Interface representing the options for an Exception.
|
|
5
|
+
*
|
|
6
|
+
* @template TCode - The type of the error code.
|
|
7
|
+
* @extends ErrorOptions
|
|
8
|
+
*/
|
|
3
9
|
interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions {
|
|
10
|
+
/** The error code associated with the Exception. */
|
|
4
11
|
code: TCode;
|
|
12
|
+
/** The name of the Exception. */
|
|
5
13
|
name?: string;
|
|
14
|
+
/** The HTTP status code associated with the Exception. */
|
|
6
15
|
status?: number;
|
|
7
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Exception Class.
|
|
19
|
+
*
|
|
20
|
+
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
21
|
+
* @template TCode The type of the code property of the Exception. Defaults to `ErrorCode`.
|
|
22
|
+
*
|
|
23
|
+
* @extends {Error}
|
|
24
|
+
* @implements {ExceptionOptions<TCode>}
|
|
25
|
+
*/
|
|
8
26
|
declare class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode> {
|
|
9
27
|
message: TMessage;
|
|
10
28
|
status: number | undefined;
|
|
11
29
|
name: string;
|
|
12
30
|
typename: 'Exception';
|
|
13
31
|
code: TCode;
|
|
32
|
+
/**
|
|
33
|
+
* Constructs a new Exception instance.
|
|
34
|
+
*
|
|
35
|
+
* @param message The message describing the exception.
|
|
36
|
+
* @param options Additional options for the exception.
|
|
37
|
+
*/
|
|
14
38
|
constructor(message: TMessage, options: ExceptionOptions<TCode>);
|
|
39
|
+
/**
|
|
40
|
+
* Determines if the provided error is an instance of the Exception class.
|
|
41
|
+
*
|
|
42
|
+
* @template TMessage The type of the message property of the Exception.
|
|
43
|
+
* @template TCode The type of the code property of the Exception.
|
|
44
|
+
*
|
|
45
|
+
* @param error The error to check.
|
|
46
|
+
* @returns `true` if the error is an instance of Exception or has a `typename` property equal to 'Exception', `false` otherwise.
|
|
47
|
+
*/
|
|
15
48
|
static isException<TMessage = string, TCode = ErrorCode>(error: any): error is Exception<TMessage, TCode>;
|
|
49
|
+
/**
|
|
50
|
+
* Converts the instance to a JSON object.
|
|
51
|
+
*
|
|
52
|
+
* @returns A JSON representation of the instance, including the message property.
|
|
53
|
+
*/
|
|
54
|
+
toJSON(): this & {
|
|
55
|
+
message: TMessage;
|
|
56
|
+
cause: unknown;
|
|
57
|
+
};
|
|
16
58
|
}
|
|
17
59
|
|
|
18
|
-
export { type ExceptionOptions
|
|
60
|
+
export { Exception, type ExceptionOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,60 @@
|
|
|
1
|
-
import ErrorCode from './code.js';
|
|
1
|
+
import { ErrorCode } from './code.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Interface representing the options for an Exception.
|
|
5
|
+
*
|
|
6
|
+
* @template TCode - The type of the error code.
|
|
7
|
+
* @extends ErrorOptions
|
|
8
|
+
*/
|
|
3
9
|
interface ExceptionOptions<TCode = ErrorCode> extends ErrorOptions {
|
|
10
|
+
/** The error code associated with the Exception. */
|
|
4
11
|
code: TCode;
|
|
12
|
+
/** The name of the Exception. */
|
|
5
13
|
name?: string;
|
|
14
|
+
/** The HTTP status code associated with the Exception. */
|
|
6
15
|
status?: number;
|
|
7
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Exception Class.
|
|
19
|
+
*
|
|
20
|
+
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
21
|
+
* @template TCode The type of the code property of the Exception. Defaults to `ErrorCode`.
|
|
22
|
+
*
|
|
23
|
+
* @extends {Error}
|
|
24
|
+
* @implements {ExceptionOptions<TCode>}
|
|
25
|
+
*/
|
|
8
26
|
declare class Exception<TMessage = string, TCode = ErrorCode> extends Error implements ExceptionOptions<TCode> {
|
|
9
27
|
message: TMessage;
|
|
10
28
|
status: number | undefined;
|
|
11
29
|
name: string;
|
|
12
30
|
typename: 'Exception';
|
|
13
31
|
code: TCode;
|
|
32
|
+
/**
|
|
33
|
+
* Constructs a new Exception instance.
|
|
34
|
+
*
|
|
35
|
+
* @param message The message describing the exception.
|
|
36
|
+
* @param options Additional options for the exception.
|
|
37
|
+
*/
|
|
14
38
|
constructor(message: TMessage, options: ExceptionOptions<TCode>);
|
|
39
|
+
/**
|
|
40
|
+
* Determines if the provided error is an instance of the Exception class.
|
|
41
|
+
*
|
|
42
|
+
* @template TMessage The type of the message property of the Exception.
|
|
43
|
+
* @template TCode The type of the code property of the Exception.
|
|
44
|
+
*
|
|
45
|
+
* @param error The error to check.
|
|
46
|
+
* @returns `true` if the error is an instance of Exception or has a `typename` property equal to 'Exception', `false` otherwise.
|
|
47
|
+
*/
|
|
15
48
|
static isException<TMessage = string, TCode = ErrorCode>(error: any): error is Exception<TMessage, TCode>;
|
|
49
|
+
/**
|
|
50
|
+
* Converts the instance to a JSON object.
|
|
51
|
+
*
|
|
52
|
+
* @returns A JSON representation of the instance, including the message property.
|
|
53
|
+
*/
|
|
54
|
+
toJSON(): this & {
|
|
55
|
+
message: TMessage;
|
|
56
|
+
cause: unknown;
|
|
57
|
+
};
|
|
16
58
|
}
|
|
17
59
|
|
|
18
|
-
export { type ExceptionOptions
|
|
60
|
+
export { Exception, type ExceptionOptions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
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;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/exception",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Handle errors with ease",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -41,26 +41,33 @@
|
|
|
41
41
|
"sideEffects": false,
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "pnpm lint && pnpm test:ci && tsup",
|
|
44
|
+
"build:watch": "tsup --watch",
|
|
44
45
|
"lint": "eslint",
|
|
45
|
-
"
|
|
46
|
-
"test
|
|
46
|
+
"release": "node scripts/publish.js --verbose --npm",
|
|
47
|
+
"test": "pnpm test:watch",
|
|
48
|
+
"test:watch": "jest --watchAll --verbose",
|
|
49
|
+
"test:jsdom": "JSDOM=true pnpm test:watch",
|
|
47
50
|
"test:ci": "jest --ci --verbose",
|
|
48
51
|
"test:ci:jsdom": "JSDOM=true pnpm test:ci",
|
|
52
|
+
"test:coverage": "pnpm test:watch --coverage",
|
|
53
|
+
"test:ci:coverage": "pnpm test:ci --coverage",
|
|
49
54
|
"test:serve-coverage": "http-server ./coverage/lcov-report --gzip true -p 0 -o --silent",
|
|
50
|
-
"test:coverage": "concurrently --prefix none --kill-others \"pnpm
|
|
55
|
+
"test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\"",
|
|
51
56
|
"test:coverage:jsdom": "JSDOM=true pnpm test:coverage",
|
|
52
|
-
"test:
|
|
57
|
+
"test:coverage:jsdom:serve": "JSDOM=true pnpm test:coverage:serve",
|
|
58
|
+
"test:jest": "pnpm test:watch jest.test.ts",
|
|
53
59
|
"test:jest:jsdom": "JSDOM=true pnpm test:jest",
|
|
54
60
|
"test:exception": "pnpm test exception.test.ts"
|
|
55
61
|
},
|
|
56
62
|
"devDependencies": {
|
|
63
|
+
"@alessiofrittoli/node-scripts": "^2.2.1",
|
|
57
64
|
"@eslint/js": "^9.17.0",
|
|
58
65
|
"@jest/globals": "^29.7.0",
|
|
59
66
|
"@types/jest": "^29.5.14",
|
|
60
67
|
"@types/node": "^22.10.2",
|
|
61
|
-
"concurrently": "^9.1.
|
|
68
|
+
"concurrently": "^9.1.2",
|
|
62
69
|
"dotenv": "^16.4.7",
|
|
63
|
-
"esbuild": "0.24.
|
|
70
|
+
"esbuild": "0.24.2",
|
|
64
71
|
"eslint": "^9.17.0",
|
|
65
72
|
"globals": "^15.14.0",
|
|
66
73
|
"http-server": "^14.1.1",
|
|
@@ -70,6 +77,6 @@
|
|
|
70
77
|
"ts-node": "^10.9.2",
|
|
71
78
|
"tsup": "^8.3.5",
|
|
72
79
|
"typescript": "^5.7.2",
|
|
73
|
-
"typescript-eslint": "^8.
|
|
80
|
+
"typescript-eslint": "^8.19.0"
|
|
74
81
|
}
|
|
75
82
|
}
|