@alessiofrittoli/exception 0.1.0 → 1.0.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 +110 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
# Exception 🚦
|
|
2
2
|
|
|
3
|
-
Version
|
|
3
|
+
Version 1.0.0
|
|
4
4
|
|
|
5
5
|
## Handle errors with ease
|
|
6
6
|
|
|
7
|
+
This documentation describes the `Exception` class, which provides a structured way to handle errors in TypeScript. It includes custom properties such as `code`, `name`, and `status` for more detailed error reporting and debugging.
|
|
8
|
+
|
|
7
9
|
### Table of Contents
|
|
8
10
|
|
|
9
11
|
- [Getting started](#getting-started)
|
|
12
|
+
- [API Reference](#api-reference)
|
|
13
|
+
- [ExceptionOptions Interface](#exceptionoptions-interface)
|
|
14
|
+
- [Exception Class](#exception-class)
|
|
15
|
+
- [Usage Scenarios](#usage-scenarios)
|
|
10
16
|
- [Development](#development)
|
|
11
17
|
- [ESLint](#eslint)
|
|
12
18
|
- [Jest](#jest)
|
|
@@ -16,6 +22,10 @@ Version 0.1.0
|
|
|
16
22
|
|
|
17
23
|
---
|
|
18
24
|
|
|
25
|
+
### Overview
|
|
26
|
+
|
|
27
|
+
The `Exception` class extends the native JavaScript `Error` object, adding customizable fields to improve error classification and handling. It also includes a static utility method to check if an object is an instance of the `Exception` class.
|
|
28
|
+
|
|
19
29
|
### Getting started
|
|
20
30
|
|
|
21
31
|
Run the following command to start using `@alessiofrittoli/exception` in your projects:
|
|
@@ -32,6 +42,105 @@ pnpm i @alessiofrittoli/exception
|
|
|
32
42
|
|
|
33
43
|
---
|
|
34
44
|
|
|
45
|
+
### API Reference
|
|
46
|
+
|
|
47
|
+
#### `ExceptionOptions` Interface
|
|
48
|
+
|
|
49
|
+
The `ExceptionOptions` interface defines the optional parameters for the `Exception` class constructor.
|
|
50
|
+
|
|
51
|
+
##### Properties
|
|
52
|
+
|
|
53
|
+
| Property | Type | Default | Description |
|
|
54
|
+
|----------|----------|-------------|------------------------------------------------------------------|
|
|
55
|
+
| `code` | `TCode` | - | (Required) A numeric or custom code representing the error type. |
|
|
56
|
+
| `name` | `string` | `Exception` | (Optional) A string representing the error name. |
|
|
57
|
+
| `status` | `number` | - | (Optional) An HTTP status code associated with the error. |
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
#### `Exception` Class
|
|
62
|
+
|
|
63
|
+
The `Exception` class extends the `Error` object and implements the `ExceptionOptions` interface.
|
|
64
|
+
|
|
65
|
+
##### Constructor
|
|
66
|
+
|
|
67
|
+
The constructor initializes an `Exception` instance with a custom message and options.
|
|
68
|
+
|
|
69
|
+
###### Parameters
|
|
70
|
+
|
|
71
|
+
| Parameter | Type | Description |
|
|
72
|
+
|-----------|--------------------|-----------------------------------------------------------------|
|
|
73
|
+
| `message` | `TMessage` | (Required) The error message. Can be a string or a custom type. |
|
|
74
|
+
| `options` | `ExceptionOptions` | (Required) An object containing the error options. |
|
|
75
|
+
|
|
76
|
+
###### Example
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import Exception from '@alessiofrittoli/exception'
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
throw new Exception( 'Resource not found', {
|
|
83
|
+
code : 'ERRNOTFOUND',
|
|
84
|
+
status : 404,
|
|
85
|
+
} )
|
|
86
|
+
} catch ( error ) {
|
|
87
|
+
console.error( error )
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
##### Static Methods
|
|
92
|
+
|
|
93
|
+
###### `isException`
|
|
94
|
+
|
|
95
|
+
A utility method to check if an object is an instance of the `Exception` class.
|
|
96
|
+
|
|
97
|
+
###### Example
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
try {
|
|
101
|
+
throw new Exception( 'Something went wrong', { code: 'ERRUNKNOWN' } )
|
|
102
|
+
} catch ( error ) {
|
|
103
|
+
if ( Exception.isException( error ) ) {
|
|
104
|
+
// we can safely access `Exception` properties
|
|
105
|
+
console.error( `Error [${ error.code }]: ${ error.message }` )
|
|
106
|
+
} else {
|
|
107
|
+
console.error( error )
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
#### Usage Scenarios
|
|
115
|
+
|
|
116
|
+
##### Custom Error Handling
|
|
117
|
+
|
|
118
|
+
The `Exception` class is ideal for creating domain-specific errors with additional metadata, such as error codes and HTTP statuses.
|
|
119
|
+
|
|
120
|
+
###### Example
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
enum ErrorCode
|
|
124
|
+
{
|
|
125
|
+
EMPTY_VALUE = 'ERREMPTYVALUE',
|
|
126
|
+
WRONG_FORMAT= 'ERRWRONGFORMAT',
|
|
127
|
+
EXPIRED = 'ERREXPIRED',
|
|
128
|
+
TOO_EARLY = 'ERRTOOEARLY',
|
|
129
|
+
NOT_ALLOWED = 'ERRNOTALLOWED',
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
await fetch( ... )
|
|
134
|
+
} catch ( error ) {
|
|
135
|
+
// error thrown by the server: Exception( 'Wrong value.', { code: ErrorCode.WRONG_FORMAT, status: 422 } )
|
|
136
|
+
if ( Exception.isException<string, ErrorCode>( error ) ) {
|
|
137
|
+
console.log( error.code ) // `error.code` is type of `ErrorCode`.
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
35
144
|
### Development
|
|
36
145
|
|
|
37
146
|
#### Install depenendencies
|
package/dist/index.d.mts
CHANGED
|
@@ -3,12 +3,13 @@ interface ExceptionOptions<TCode = number> extends ErrorOptions {
|
|
|
3
3
|
name?: string;
|
|
4
4
|
status?: number;
|
|
5
5
|
}
|
|
6
|
-
declare class Exception<TCode = number> extends Error implements ExceptionOptions<TCode> {
|
|
6
|
+
declare class Exception<TMessage = string, TCode = number> extends Error implements ExceptionOptions<TCode> {
|
|
7
|
+
message: TMessage;
|
|
7
8
|
status: number | undefined;
|
|
8
9
|
name: string;
|
|
9
10
|
code: TCode;
|
|
10
|
-
constructor(message:
|
|
11
|
-
static isException<
|
|
11
|
+
constructor(message: TMessage, options: ExceptionOptions<TCode>);
|
|
12
|
+
static isException<TMessage = string, TCode = number>(error: any): error is Exception<TMessage, TCode>;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export { type ExceptionOptions, Exception as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,13 @@ interface ExceptionOptions<TCode = number> extends ErrorOptions {
|
|
|
3
3
|
name?: string;
|
|
4
4
|
status?: number;
|
|
5
5
|
}
|
|
6
|
-
declare class Exception<TCode = number> extends Error implements ExceptionOptions<TCode> {
|
|
6
|
+
declare class Exception<TMessage = string, TCode = number> extends Error implements ExceptionOptions<TCode> {
|
|
7
|
+
message: TMessage;
|
|
7
8
|
status: number | undefined;
|
|
8
9
|
name: string;
|
|
9
10
|
code: TCode;
|
|
10
|
-
constructor(message:
|
|
11
|
-
static isException<
|
|
11
|
+
constructor(message: TMessage, options: ExceptionOptions<TCode>);
|
|
12
|
+
static isException<TMessage = string, TCode = number>(error: any): error is Exception<TMessage, TCode>;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export { type ExceptionOptions, Exception as default };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var t=class n extends Error{message;status;name;code;constructor(s,e){super("",e),this.name=e.name||"Exception",this.message=s,this.code=e.code,this.status=e.status;}static isException(s){return s instanceof n}},o=t;module.exports=o;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var t=class n extends Error{message;status;name;code;constructor(s,e){super("",e),this.name=e.name||"Exception",this.message=s,this.code=e.code,this.status=e.status;}static isException(s){return s instanceof n}},a=t;export{a as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/exception",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Handle errors with ease",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -29,14 +29,15 @@
|
|
|
29
29
|
],
|
|
30
30
|
"sideEffects": false,
|
|
31
31
|
"scripts": {
|
|
32
|
-
"build": "eslint && pnpm test:ci &&
|
|
32
|
+
"build": "eslint && pnpm test:ci && tsup",
|
|
33
33
|
"lint": "eslint",
|
|
34
34
|
"test": "jest --watchAll --verbose",
|
|
35
35
|
"test:jsdom": "JSDOM=true pnpm test",
|
|
36
36
|
"test:ci": "jest --ci --verbose",
|
|
37
37
|
"test:ci:jsdom": "JSDOM=true pnpm test:ci",
|
|
38
38
|
"test:jest": "pnpm test jest.test.ts",
|
|
39
|
-
"test:jest:jsdom": "JSDOM=true pnpm test:jest"
|
|
39
|
+
"test:jest:jsdom": "JSDOM=true pnpm test:jest",
|
|
40
|
+
"test:exception": "pnpm test exception.test.ts"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"@eslint/js": "^9.17.0",
|