@alessiofrittoli/exception 3.5.0 → 4.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 +190 -249
- package/dist/abort/index.cjs +1 -0
- package/dist/abort/index.d.cts +29 -0
- package/dist/abort/index.d.mts +29 -0
- package/dist/abort/index.mjs +1 -0
- package/dist/code/index.cjs +1 -0
- package/dist/code/index.d.cts +19 -0
- package/dist/code/index.d.mts +19 -0
- package/dist/code/index.mjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +177 -0
- package/dist/index.d.mts +164 -58
- package/dist/index.mjs +1 -1
- package/license.md +1 -1
- package/package.json +34 -45
- package/dist/abort.d.mts +0 -35
- package/dist/abort.d.ts +0 -35
- package/dist/abort.js +0 -1
- package/dist/abort.mjs +0 -1
- package/dist/chunk-OBRRZX4D.js +0 -1
- package/dist/chunk-PSRQLNM6.js +0 -1
- package/dist/chunk-RAHIXT6K.mjs +0 -1
- package/dist/chunk-VBWDYSF3.mjs +0 -1
- package/dist/code.d.mts +0 -19
- package/dist/code.d.ts +0 -19
- package/dist/code.js +0 -1
- package/dist/code.mjs +0 -1
- package/dist/index.d.ts +0 -71
- package/dist/index.js +0 -1
package/README.md
CHANGED
|
@@ -1,42 +1,65 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">Exception 🚦</h1>
|
|
2
|
+
<h2 align="center">
|
|
3
|
+
Handle errors with ease
|
|
4
|
+
</h2>
|
|
5
|
+
<p align="center">
|
|
6
|
+
This documentation describes the <code>Exception</code> class, which provides a structured way to handle errors in TypeScript. It includes custom properties such as <code>code</code>, <code>name</code>, and <code>status</code> for more detailed error reporting and debugging.
|
|
7
|
+
</p>
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="https://npmjs.org/package/@alessiofrittoli/exception">
|
|
10
|
+
<img src="https://img.shields.io/npm/v/@alessiofrittoli/exception" alt="Latest version"/>
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://coveralls.io/github/alessiofrittoli/node-scripts">
|
|
13
|
+
<img src="https://coveralls.io/repos/github/alessiofrittoli/node-scripts/badge.svg" alt="Test coverage"/>
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://socket.dev/npm/package/@alessiofrittoli/exception/overview">
|
|
16
|
+
<img src="https://socket.dev/api/badge/npm/package/@alessiofrittoli/exception" alt="Socket Security score"/>
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://npmjs.org/package/@alessiofrittoli/exception">
|
|
19
|
+
<img src="https://img.shields.io/npm/dm/@alessiofrittoli/exception.svg" alt="npm downloads"/>
|
|
20
|
+
</a>
|
|
21
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/exception">
|
|
22
|
+
<img src="https://badgen.net/bundlephobia/dependency-count/@alessiofrittoli/exception" alt="Dependencies"/>
|
|
23
|
+
</a>
|
|
24
|
+
<a href="https://libraries.io/npm/%40alessiofrittoli%2Fnode-scripts">
|
|
25
|
+
<img src="https://img.shields.io/librariesio/release/npm/@alessiofrittoli/exception" alt="Dependencies status"/>
|
|
26
|
+
</a>
|
|
27
|
+
</p>
|
|
28
|
+
<p align="center">
|
|
29
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/exception">
|
|
30
|
+
<img src="https://badgen.net/bundlephobia/min/@alessiofrittoli/exception" alt="minified"/>
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/exception">
|
|
33
|
+
<img src="https://badgen.net/bundlephobia/minzip/@alessiofrittoli/exception" alt="minizipped"/>
|
|
34
|
+
</a>
|
|
35
|
+
<a href="https://bundlephobia.com/package/@alessiofrittoli/exception">
|
|
36
|
+
<img src="https://badgen.net/bundlephobia/tree-shaking/@alessiofrittoli/exception" alt="Tree shakable"/>
|
|
37
|
+
</a>
|
|
38
|
+
</p>
|
|
39
|
+
<p align="center">
|
|
40
|
+
<a href="https://github.com/sponsors/alessiofrittoli">
|
|
41
|
+
<img src="https://img.shields.io/static/v1?label=Fund%20this%20package&message=%E2%9D%A4&logo=GitHub&color=%23DB61A2" alt="Fund this package"/>
|
|
42
|
+
</a>
|
|
43
|
+
</p>
|
|
2
44
|
|
|
3
|
-
[![NPM Latest Version][version-badge]][npm-url] [![Coverage Status][coverage-badge]][coverage-url] [![Socket Status][socket-badge]][socket-url] [![NPM Monthly Downloads][downloads-badge]][npm-url] [![Dependencies][deps-badge]][deps-url]
|
|
4
|
-
|
|
5
|
-
[![GitHub Sponsor][sponsor-badge]][sponsor-url]
|
|
6
|
-
|
|
7
|
-
[version-badge]: https://img.shields.io/npm/v/%40alessiofrittoli%2Fexception
|
|
8
|
-
[npm-url]: https://npmjs.org/package/%40alessiofrittoli%2Fexception
|
|
9
|
-
[coverage-badge]: https://coveralls.io/repos/github/alessiofrittoli/exception/badge.svg
|
|
10
|
-
[coverage-url]: https://coveralls.io/github/alessiofrittoli/exception
|
|
11
|
-
[socket-badge]: https://socket.dev/api/badge/npm/package/@alessiofrittoli/exception
|
|
12
|
-
[socket-url]: https://socket.dev/npm/package/@alessiofrittoli/exception/overview
|
|
13
|
-
[downloads-badge]: https://img.shields.io/npm/dm/%40alessiofrittoli%2Fexception.svg
|
|
14
|
-
[deps-badge]: https://img.shields.io/librariesio/release/npm/%40alessiofrittoli%2Fexception
|
|
15
|
-
[deps-url]: https://libraries.io/npm/%40alessiofrittoli%2Fexception
|
|
16
45
|
[sponsor-badge]: https://img.shields.io/static/v1?label=Fund%20this%20package&message=%E2%9D%A4&logo=GitHub&color=%23DB61A2
|
|
17
46
|
[sponsor-url]: https://github.com/sponsors/alessiofrittoli
|
|
18
47
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
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.
|
|
22
|
-
|
|
23
|
-
### Table of Contents
|
|
48
|
+
### Table of Content
|
|
24
49
|
|
|
25
50
|
- [Getting started](#getting-started)
|
|
51
|
+
- [Development](#development)
|
|
52
|
+
- [Local development](#local-development)
|
|
53
|
+
- [Production build](#production-build)
|
|
54
|
+
- [Unit tests](#unit-tests)
|
|
55
|
+
- [Run tests with coverage](#run-tests-with-coverage)
|
|
56
|
+
- [Contributing](#contributing)
|
|
57
|
+
- [Security](#security)
|
|
26
58
|
- [API Reference](#api-reference)
|
|
27
59
|
- [ExceptionOptions Interface](#exceptionoptions-interface)
|
|
28
60
|
- [Exception Class](#exception-class)
|
|
29
61
|
- [AbortError Class](#aborterror-class)
|
|
30
62
|
- [AbortError Usage Scenarios](#aborterror-usage-scenarios)
|
|
31
|
-
- [`ErrorCode` enum](#errorcode-enum)
|
|
32
|
-
- [Examples](#examples)
|
|
33
|
-
- [Development](#development)
|
|
34
|
-
- [Install depenendencies](#install-depenendencies)
|
|
35
|
-
- [Build the source code](#build-the-source-code)
|
|
36
|
-
- [ESLint](#eslint)
|
|
37
|
-
- [Jest](#jest)
|
|
38
|
-
- [Contributing](#contributing)
|
|
39
|
-
- [Security](#security)
|
|
40
63
|
- [Credits](#made-with-)
|
|
41
64
|
|
|
42
65
|
---
|
|
@@ -61,6 +84,84 @@ pnpm i @alessiofrittoli/exception
|
|
|
61
84
|
|
|
62
85
|
---
|
|
63
86
|
|
|
87
|
+
### Development
|
|
88
|
+
|
|
89
|
+
Run the following to start development
|
|
90
|
+
|
|
91
|
+
```shell
|
|
92
|
+
nvm use
|
|
93
|
+
|
|
94
|
+
pnpm i
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Local development
|
|
98
|
+
|
|
99
|
+
Run the following
|
|
100
|
+
|
|
101
|
+
```shell
|
|
102
|
+
pnpm dev
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
This will generate unminified output code, sourcemaps and will enable source file watcher.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
#### Production build
|
|
110
|
+
|
|
111
|
+
Run the following to create a local production build
|
|
112
|
+
|
|
113
|
+
```shell
|
|
114
|
+
pnpm build
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
This will generate minified output code without sourcemaps.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
#### Unit tests
|
|
122
|
+
|
|
123
|
+
Run all the defined test suites by running the following:
|
|
124
|
+
|
|
125
|
+
```shell
|
|
126
|
+
# Run tests and watch file changes.
|
|
127
|
+
pnpm test:watch
|
|
128
|
+
|
|
129
|
+
# Run tests in a CI environment.
|
|
130
|
+
pnpm test:ci
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
##### Run tests with coverage
|
|
136
|
+
|
|
137
|
+
An HTTP server is then started to serve coverage files from `./coverage` folder.
|
|
138
|
+
|
|
139
|
+
⚠️ You may see a blank page the first time you run this command. Simply refresh the browser to see the updates.
|
|
140
|
+
|
|
141
|
+
```shell
|
|
142
|
+
pnpm test:coverage:serve
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
#### Contributing
|
|
148
|
+
|
|
149
|
+
Contributions are truly welcome!
|
|
150
|
+
|
|
151
|
+
Please refer to the [Contributing Doc](./CONTRIBUTING.md) for more information on how to start contributing to this project.
|
|
152
|
+
|
|
153
|
+
Help keep this project up to date with [GitHub Sponsor][sponsor-url].
|
|
154
|
+
|
|
155
|
+
[![GitHub Sponsor][sponsor-badge]][sponsor-url]
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
#### Security
|
|
160
|
+
|
|
161
|
+
If you believe you have found a security vulnerability, we encourage you to **_responsibly disclose this and NOT open a public issue_**. We will investigate all legitimate reports. Email `security@alessiofrittoli.it` to disclose any security vulnerabilities.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
64
165
|
### API Reference
|
|
65
166
|
|
|
66
167
|
#### `ExceptionOptions` Interface
|
|
@@ -69,11 +170,16 @@ The `ExceptionOptions` interface defines the optional parameters for the `Except
|
|
|
69
170
|
|
|
70
171
|
##### Properties
|
|
71
172
|
|
|
72
|
-
| Property
|
|
73
|
-
|
|
|
74
|
-
| `
|
|
75
|
-
| `
|
|
76
|
-
| `
|
|
173
|
+
| Property | Type | Default | Description |
|
|
174
|
+
| ------------- | --------- | ------------------------- | ------------------------------------------------------------------------------- |
|
|
175
|
+
| `name` | `string` | `ExceptionName.Exception` | (Optional) The error interface name. |
|
|
176
|
+
| `message` | `string` | "Unknown error." | (Optional) The error message containing technical informations about the error. |
|
|
177
|
+
| `title` | `string` | - | (Optional) The error title. |
|
|
178
|
+
| `description` | `string` | - | (Optional) The error description. |
|
|
179
|
+
| `code` | `T` | - | (Required) The error code associated with the Exception. |
|
|
180
|
+
| `isCritical` | `boolean` | `false` | (Optional) Indicates whether the error is a critical error. |
|
|
181
|
+
| `status` | `number` | - | (Optional) The HTTP status code associated with the Exception. |
|
|
182
|
+
| `cause` | `unknown` | - | (Optional) The error cause. |
|
|
77
183
|
|
|
78
184
|
---
|
|
79
185
|
|
|
@@ -87,23 +193,23 @@ The constructor initializes an `Exception` instance with a custom message and op
|
|
|
87
193
|
|
|
88
194
|
###### Parameters
|
|
89
195
|
|
|
90
|
-
| Parameter | Type | Description
|
|
91
|
-
| --------- | ------------------ |
|
|
92
|
-
| `
|
|
93
|
-
| `options` | `ExceptionOptions` | (Required) An object containing the error options. |
|
|
196
|
+
| Parameter | Type | Description |
|
|
197
|
+
| --------- | ------------------ | -------------------------------------------------- |
|
|
198
|
+
| `options` | `ExceptionOptions` | (Required) An object containing the error options. |
|
|
94
199
|
|
|
95
200
|
###### Example
|
|
96
201
|
|
|
97
202
|
```ts
|
|
98
|
-
import { Exception } from
|
|
203
|
+
import { Exception } from '@alessiofrittoli/exception'
|
|
99
204
|
|
|
100
205
|
try {
|
|
101
|
-
throw new Exception(
|
|
102
|
-
|
|
206
|
+
throw new Exception({
|
|
207
|
+
message: 'Resource not found'
|
|
208
|
+
code: 'ERR:NOTFOUND',
|
|
103
209
|
status: 404,
|
|
104
|
-
})
|
|
210
|
+
})
|
|
105
211
|
} catch (error) {
|
|
106
|
-
console.error(error)
|
|
212
|
+
console.error(error)
|
|
107
213
|
}
|
|
108
214
|
```
|
|
109
215
|
|
|
@@ -119,29 +225,33 @@ It supports also a JSON representation of the `Exception` class (commonly return
|
|
|
119
225
|
|
|
120
226
|
```ts
|
|
121
227
|
try {
|
|
122
|
-
throw new Exception(
|
|
228
|
+
throw new Exception({ message: 'Something went wrong', code: 'ERR:UNKNOWN' })
|
|
123
229
|
} catch (error) {
|
|
124
230
|
if (Exception.isException(error)) {
|
|
125
231
|
// we can safely access `Exception` properties
|
|
126
|
-
console.error(`Error [${error.code}]: ${error.message}`)
|
|
232
|
+
console.error(`Error [${error.code}]: ${error.message}`)
|
|
127
233
|
} else {
|
|
128
|
-
console.error(error)
|
|
234
|
+
console.error(error)
|
|
129
235
|
}
|
|
130
236
|
}
|
|
131
237
|
```
|
|
132
238
|
|
|
133
239
|
```ts
|
|
240
|
+
import { ExceptionName } from '@alessiofrittoli/exception'
|
|
241
|
+
|
|
134
242
|
/** Simulates JSON Exception returned by a server JSON Response. */
|
|
135
243
|
const error = JSON.parse(
|
|
136
244
|
JSON.stringify(
|
|
137
|
-
new Exception(
|
|
245
|
+
new Exception({
|
|
246
|
+
name: ExceptionName.AbortError,
|
|
247
|
+
message: 'Exception with custom name.',
|
|
138
248
|
code: 0,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
);
|
|
249
|
+
}),
|
|
250
|
+
),
|
|
251
|
+
)
|
|
143
252
|
|
|
144
|
-
console.log(Exception.isException(error))
|
|
253
|
+
console.log(Exception.isException(error)) // Outputs: true
|
|
254
|
+
console.log(new Exception(error)) // Outputs: [Exception]
|
|
145
255
|
```
|
|
146
256
|
|
|
147
257
|
---
|
|
@@ -156,10 +266,11 @@ The constructor initializes an `AbortError` instance with a custom message and o
|
|
|
156
266
|
|
|
157
267
|
###### Parameters
|
|
158
268
|
|
|
159
|
-
| Parameter | Type | Description
|
|
160
|
-
| --------- | ------------------- |
|
|
161
|
-
| `
|
|
162
|
-
|
|
|
269
|
+
| Parameter | Type | Description |
|
|
270
|
+
| --------- | ------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
271
|
+
| `options` | `AbortErrorOptions` | An object containing the error options. |
|
|
272
|
+
| | | It extends the [`ExceptionOptions` Interface](#exceptionoptions-interface) and omit the `name` property. |
|
|
273
|
+
| | | The `code` defaults to `ErrorCode.ABORT` but can be optionally customized. |
|
|
163
274
|
|
|
164
275
|
---
|
|
165
276
|
|
|
@@ -174,24 +285,24 @@ try {
|
|
|
174
285
|
const controller = new AbortController()
|
|
175
286
|
const { signal } = controller
|
|
176
287
|
|
|
177
|
-
button.addEventListener(
|
|
178
|
-
controller.abort(
|
|
179
|
-
}
|
|
288
|
+
button.addEventListener('click', () => {
|
|
289
|
+
controller.abort(new AbortError({ message: 'User cancelled the request.' }))
|
|
290
|
+
})
|
|
180
291
|
|
|
181
|
-
await fetch(
|
|
182
|
-
} catch (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if ( AbortError.isAbortError( error ) ) {
|
|
292
|
+
await fetch(..., { signal })
|
|
293
|
+
} catch (error) {
|
|
294
|
+
if (AbortError.isException(error)) {
|
|
295
|
+
if (AbortError.isAbortError(error)) {
|
|
186
296
|
// handle abort error
|
|
187
|
-
console.log(
|
|
297
|
+
console.log(error.code) // `error.code` is type of `ErrorCode.ABORT`.
|
|
188
298
|
return
|
|
189
299
|
}
|
|
300
|
+
|
|
190
301
|
// handle other Exception errors
|
|
191
302
|
return
|
|
192
303
|
}
|
|
193
|
-
// handle other unknown errors
|
|
194
304
|
|
|
305
|
+
// handle other unknown errors
|
|
195
306
|
}
|
|
196
307
|
```
|
|
197
308
|
|
|
@@ -202,8 +313,7 @@ try {
|
|
|
202
313
|
```ts
|
|
203
314
|
import { AbortError } from '@alessiofrittoli/exception/abort'
|
|
204
315
|
|
|
205
|
-
enum
|
|
206
|
-
{
|
|
316
|
+
enum ApplicationErrorCode {
|
|
207
317
|
CUSTOM_CODE = 'ERR:CUSTOM_ABORT_CODE',
|
|
208
318
|
}
|
|
209
319
|
|
|
@@ -211,203 +321,34 @@ try {
|
|
|
211
321
|
const controller = new AbortController()
|
|
212
322
|
const { signal } = controller
|
|
213
323
|
|
|
214
|
-
button.addEventListener(
|
|
324
|
+
button.addEventListener('click', () => {
|
|
215
325
|
controller.abort(
|
|
216
|
-
new AbortError(
|
|
326
|
+
new AbortError({
|
|
327
|
+
message: 'User cancelled the request.',
|
|
328
|
+
code: ApplicationErrorCode.CUSTOM_CODE,
|
|
329
|
+
}),
|
|
217
330
|
)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
await fetch( ..., { signal } )
|
|
221
|
-
} catch ( error ) {
|
|
331
|
+
})
|
|
222
332
|
|
|
223
|
-
|
|
224
|
-
|
|
333
|
+
await fetch('', { signal })
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (AbortError.isException<ApplicationErrorCode>(error)) {
|
|
336
|
+
if (AbortError.isAbortError(error, ApplicationErrorCode.CUSTOM_CODE)) {
|
|
225
337
|
// handle abort error
|
|
226
|
-
console.log(
|
|
338
|
+
console.log(error.code) // `error.code` is type of `CustomAbortCode.CUSTOM_CODE`.
|
|
227
339
|
return
|
|
228
340
|
}
|
|
341
|
+
|
|
229
342
|
// handle other Exception errors
|
|
230
343
|
return
|
|
231
344
|
}
|
|
232
|
-
// handle other unknown errors
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
---
|
|
238
|
-
|
|
239
|
-
#### `ErrorCode` enum
|
|
240
|
-
|
|
241
|
-
The `ErrorCode` enum is a utility that provides pre-defined constants for the most common error codes.\
|
|
242
|
-
It is designed to simplify error handling and improve feedback quality returned to the user.
|
|
243
|
-
|
|
244
|
-
<details>
|
|
245
|
-
|
|
246
|
-
<summary>Constants Overview</summary>
|
|
247
|
-
|
|
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. |
|
|
260
|
-
|
|
261
|
-
</details>
|
|
262
|
-
|
|
263
|
-
---
|
|
264
345
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
<summary>Extending the `enum` in your project</summary>
|
|
268
|
-
|
|
269
|
-
For obvious reasons the default `ErrorCode` provided by this library might be not enough to cover all the error cases in your project.
|
|
270
|
-
|
|
271
|
-
To fill this gap, you can "extend" the `ErrorCode` enum by doing so:
|
|
272
|
-
|
|
273
|
-
```ts
|
|
274
|
-
// myproject/src/error-code.ts
|
|
275
|
-
import { ErrorCode as Exception } from "@alessiofrittoli/exception/code";
|
|
276
|
-
|
|
277
|
-
/** Your project custom `ErrorCode`. */
|
|
278
|
-
export const MyProjectErrorCode = {
|
|
279
|
-
INVALID_SIGN: "ERR:INVALIDSIGN",
|
|
280
|
-
} as const;
|
|
281
|
-
|
|
282
|
-
export const ErrorCode = { ...Exception, ...MyProjectErrorCode };
|
|
283
|
-
export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
</details>
|
|
287
|
-
|
|
288
|
-
---
|
|
289
|
-
|
|
290
|
-
#### Examples
|
|
291
|
-
|
|
292
|
-
##### Using the default `ErrorCode` to throw a new `Exception`
|
|
293
|
-
|
|
294
|
-
```ts
|
|
295
|
-
import { Exception } from "@alessiofrittoli/exception";
|
|
296
|
-
import { ErrorCode } from "@alessiofrittoli/exception/code";
|
|
297
|
-
|
|
298
|
-
throw new Exception("Password is a required field to log you in.", {
|
|
299
|
-
code: ErrorCode.EMPTY_VALUE,
|
|
300
|
-
status: 422,
|
|
301
|
-
});
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
##### Using custom `ErrorCode` to throw a new `Exception`
|
|
305
|
-
|
|
306
|
-
```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)
|
|
309
|
-
|
|
310
|
-
throw new Exception("Invalid signature.", {
|
|
311
|
-
code: ErrorCode.INVALID_SIGN,
|
|
312
|
-
status: 403,
|
|
313
|
-
});
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
##### Using `ErrorCode` to handle errors
|
|
317
|
-
|
|
318
|
-
```ts
|
|
319
|
-
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts` (see - Extending the `enum` in your project)
|
|
320
|
-
|
|
321
|
-
try {
|
|
322
|
-
...
|
|
323
|
-
} catch ( error ) {
|
|
324
|
-
if ( Exception.isException<string, ErrorCode>( error ) ) {
|
|
325
|
-
switch ( error.code ) { // `error.code` is now type of `ErrorCode` (custom).
|
|
326
|
-
case ErrorCode.INVALID_SIGN:
|
|
327
|
-
console.log( 'The signature is not valid.' )
|
|
328
|
-
break
|
|
329
|
-
case ErrorCode.Exception.UNKNOWN:
|
|
330
|
-
default:
|
|
331
|
-
console.log( 'Unexpected error occured.' )
|
|
332
|
-
}
|
|
333
|
-
}
|
|
346
|
+
// handle other unknown errors
|
|
334
347
|
}
|
|
335
348
|
```
|
|
336
349
|
|
|
337
350
|
---
|
|
338
351
|
|
|
339
|
-
### Development
|
|
340
|
-
|
|
341
|
-
#### Install depenendencies
|
|
342
|
-
|
|
343
|
-
```bash
|
|
344
|
-
npm install
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
or using `pnpm`
|
|
348
|
-
|
|
349
|
-
```bash
|
|
350
|
-
pnpm i
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
#### Build the source code
|
|
354
|
-
|
|
355
|
-
Run the following command to test and build code for distribution.
|
|
356
|
-
|
|
357
|
-
```bash
|
|
358
|
-
pnpm build
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
#### [ESLint](https://www.npmjs.com/package/eslint)
|
|
362
|
-
|
|
363
|
-
warnings / errors check.
|
|
364
|
-
|
|
365
|
-
```bash
|
|
366
|
-
pnpm lint
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
#### [Jest](https://npmjs.com/package/jest)
|
|
370
|
-
|
|
371
|
-
Run all the defined test suites by running the following:
|
|
372
|
-
|
|
373
|
-
```bash
|
|
374
|
-
# Run tests and watch file changes.
|
|
375
|
-
pnpm test:watch
|
|
376
|
-
|
|
377
|
-
# Run tests in a CI environment.
|
|
378
|
-
pnpm test:ci
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
- See [`package.json`](./package.json) file scripts for more info.
|
|
382
|
-
|
|
383
|
-
Run tests with coverage.
|
|
384
|
-
|
|
385
|
-
An HTTP server is then started to serve coverage files from `./coverage` folder.
|
|
386
|
-
|
|
387
|
-
⚠️ You may see a blank page the first time you run this command. Simply refresh the browser to see the updates.
|
|
388
|
-
|
|
389
|
-
```bash
|
|
390
|
-
test:coverage:serve
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
---
|
|
394
|
-
|
|
395
|
-
### Contributing
|
|
396
|
-
|
|
397
|
-
Contributions are truly welcome!
|
|
398
|
-
|
|
399
|
-
Please refer to the [Contributing Doc](./CONTRIBUTING.md) for more information on how to start contributing to this project.
|
|
400
|
-
|
|
401
|
-
Help keep this project up to date with [GitHub Sponsor][sponsor-url].
|
|
402
|
-
|
|
403
|
-
[![GitHub Sponsor][sponsor-badge]][sponsor-url]
|
|
404
|
-
|
|
405
|
-
---
|
|
406
|
-
|
|
407
|
-
### Security
|
|
408
|
-
|
|
409
|
-
If you believe you have found a security vulnerability, we encourage you to **_responsibly disclose this and NOT open a public issue_**. We will investigate all legitimate reports. Email `security@alessiofrittoli.it` to disclose any security vulnerabilities.
|
|
410
|
-
|
|
411
352
|
### Made with ☕
|
|
412
353
|
|
|
413
354
|
<table style='display:flex;gap:20px;'>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../code/index.cjs"),t=require("../index.cjs");var n=class extends t.Exception{constructor(n={}){let{code:r=e.ErrorCode.ABORT,...i}=n;super({...i,name:t.ExceptionName.AbortError,code:r})}static isAbortError(e,t){return super.isAbortError(e,t)}};exports.AbortError=n;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ErrorCode } from "../code/index.cjs";
|
|
2
|
+
import { Exception, ExceptionOptions } from "../index.cjs";
|
|
3
|
+
//#region src/abort/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Interface representing the options for an Exception.
|
|
6
|
+
*
|
|
7
|
+
* @extends ExceptionOptions
|
|
8
|
+
*/
|
|
9
|
+
export type AbortErrorOptions<T = ErrorCode> = Partial<Omit<ExceptionOptions<T>, 'name'>>;
|
|
10
|
+
/**
|
|
11
|
+
* Exception Class.
|
|
12
|
+
*
|
|
13
|
+
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
14
|
+
*
|
|
15
|
+
* @extends Exception
|
|
16
|
+
*/
|
|
17
|
+
export declare class AbortError<T = ErrorCode> extends Exception<T> implements AbortErrorOptions<T> {
|
|
18
|
+
constructor(options?: AbortErrorOptions<T>);
|
|
19
|
+
/**
|
|
20
|
+
* Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.
|
|
21
|
+
*
|
|
22
|
+
* @template T The type of the code property of the Exception.
|
|
23
|
+
*
|
|
24
|
+
* @param error The error to check.
|
|
25
|
+
* @returns `true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.
|
|
26
|
+
*/
|
|
27
|
+
static isAbortError<T = ErrorCode>(error: any, code?: T): error is AbortError<T>;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ErrorCode } from "../code/index.mjs";
|
|
2
|
+
import { Exception, ExceptionOptions } from "../index.mjs";
|
|
3
|
+
//#region src/abort/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Interface representing the options for an Exception.
|
|
6
|
+
*
|
|
7
|
+
* @extends ExceptionOptions
|
|
8
|
+
*/
|
|
9
|
+
export type AbortErrorOptions<T = ErrorCode> = Partial<Omit<ExceptionOptions<T>, 'name'>>;
|
|
10
|
+
/**
|
|
11
|
+
* Exception Class.
|
|
12
|
+
*
|
|
13
|
+
* @template TMessage The type of the message property of the Exception. Defaults to `string`.
|
|
14
|
+
*
|
|
15
|
+
* @extends Exception
|
|
16
|
+
*/
|
|
17
|
+
export declare class AbortError<T = ErrorCode> extends Exception<T> implements AbortErrorOptions<T> {
|
|
18
|
+
constructor(options?: AbortErrorOptions<T>);
|
|
19
|
+
/**
|
|
20
|
+
* Determines if the provided error is an instance of the AbortError class and has the ABORT ErrorCode.
|
|
21
|
+
*
|
|
22
|
+
* @template T The type of the code property of the Exception.
|
|
23
|
+
*
|
|
24
|
+
* @param error The error to check.
|
|
25
|
+
* @returns `true` if the error is an `AbortError` and has the ABORT ErrorCode, `false` otherwise.
|
|
26
|
+
*/
|
|
27
|
+
static isAbortError<T = ErrorCode>(error: any, code?: T): error is AbortError<T>;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ErrorCode as e}from"../code/index.mjs";import{Exception as t,ExceptionName as n}from"../index.mjs";var r=class extends t{constructor(t={}){let{code:r=e.ABORT,...i}=t;super({...i,name:n.AbortError,code:r})}static isAbortError(e,t){return super.isAbortError(e,t)}};export{r as AbortError};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e={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.ErrorCode=e;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/code/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Most common error codes.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare const ErrorCode: {
|
|
7
|
+
readonly UNKNOWN: "ERR:UNKNOWN";
|
|
8
|
+
readonly ABORT: "ERR:ABORT";
|
|
9
|
+
readonly EMPTY_VALUE: "ERR:EMPTYVALUE";
|
|
10
|
+
readonly WRONG_VALUE: "ERR:WRONGVALUE";
|
|
11
|
+
readonly EXPIRED: "ERR:EXPIRED";
|
|
12
|
+
readonly TOO_EARLY: "ERR:TOOEARLY";
|
|
13
|
+
readonly TOO_MANY: "ERR:TOOMANY";
|
|
14
|
+
readonly QUOTA_REACHED: "ERR:QUOTAREACHED";
|
|
15
|
+
readonly NOT_FOUND: "ERR:NOTFOUND";
|
|
16
|
+
readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
|
|
17
|
+
};
|
|
18
|
+
export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
19
|
+
//#endregion
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/code/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Most common error codes.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare const ErrorCode: {
|
|
7
|
+
readonly UNKNOWN: "ERR:UNKNOWN";
|
|
8
|
+
readonly ABORT: "ERR:ABORT";
|
|
9
|
+
readonly EMPTY_VALUE: "ERR:EMPTYVALUE";
|
|
10
|
+
readonly WRONG_VALUE: "ERR:WRONGVALUE";
|
|
11
|
+
readonly EXPIRED: "ERR:EXPIRED";
|
|
12
|
+
readonly TOO_EARLY: "ERR:TOOEARLY";
|
|
13
|
+
readonly TOO_MANY: "ERR:TOOMANY";
|
|
14
|
+
readonly QUOTA_REACHED: "ERR:QUOTAREACHED";
|
|
15
|
+
readonly NOT_FOUND: "ERR:NOTFOUND";
|
|
16
|
+
readonly OFFLINE: "ERR:INTERNETDISCONNECTED";
|
|
17
|
+
};
|
|
18
|
+
export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
19
|
+
//#endregion
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={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{e as ErrorCode};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./code/index.cjs"),t={Exception:`Exception`,AbortError:`AbortError`,NotAllowedError:`NotAllowedError`,NotFoundError:`NotFoundError`,NotSupportedError:`NotSupportedError`,InvalidStateError:`InvalidStateError`,InvalidAccessError:`InvalidAccessError`,NetworkError:`NetworkError`,SecurityError:`SecurityError`,TimeoutError:`TimeoutError`,QuotaExceededError:`QuotaExceededError`,DataError:`DataError`,EncodingError:`EncodingError`,OperationError:`OperationError`,ConstraintError:`ConstraintError`,TransactionInactiveError:`TransactionInactiveError`,ReadOnlyError:`ReadOnlyError`,VersionError:`VersionError`,NoModificationAllowedError:`NoModificationAllowedError`,HierarchyRequestError:`HierarchyRequestError`,WrongDocumentError:`WrongDocumentError`,InvalidCharacterError:`InvalidCharacterError`,NamespaceError:`NamespaceError`,SyntaxError:`SyntaxError`,DataCloneError:`DataCloneError`,URLMismatchError:`URLMismatchError`,InvalidModificationError:`InvalidModificationError`,NotReadableError:`NotReadableError`};var n=class n extends Error{__typename=t.Exception;name=this.__typename;message;title;description;code;isCritical;status;cause;constructor(e){let{message:t=`Unknown error.`}=e;super(t),e.name&&(this.name=e.name),this.message=t,this.title=e.title,this.description=e.description,this.code=e.code,this.isCritical=e.isCritical??!1,this.status=e.status,this.cause=n.isException(e.cause)?new n(e.cause):e.cause}static isException(e){return e instanceof n||typeof e==`object`&&`__typename`in e&&e.__typename===t.Exception}static isAbortError(r,i=e.ErrorCode.ABORT){return n.isException(r)&&(r.name===t.AbortError||r.code===i)}toJSON(){return{...this,message:this.message,cause:this.cause}}};exports.Exception=n,exports.ExceptionName=t;
|