error-shield 2.0.0 โ 2.0.1
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/LICENSE +15 -0
- package/README.md +335 -141
- package/dist/cjs/index.cjs +270 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.js +238 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.ts +4 -90
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +182 -0
- package/package.json +63 -28
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gopinath Kathirvel
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,36 +1,111 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# ๐ก๏ธ Error Shield
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### Elegant, structured error handling for Node.js & Express.js
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
- ๐ **TypeScript Support** - Full TypeScript definitions included
|
|
7
|
+
[](https://www.npmjs.com/package/error-shield)
|
|
8
|
+
[](https://github.com/Gopinathgopi13/error-shield/blob/main/LICENSE)
|
|
9
|
+
[](https://www.typescriptlang.org/)
|
|
10
|
+
[](https://nodejs.org/)
|
|
11
|
+
[](https://www.npmjs.com/package/error-shield)
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
**Stop writing repetitive error handling code.** Error Shield gives you a battle-tested toolkit โ
|
|
14
|
+
custom error classes, async wrappers, Express middleware, and 40+ HTTP error creators โ out of the box.
|
|
15
|
+
|
|
16
|
+
[Get Started](#-quick-start) ยท [API Reference](#-api-reference) ยท [Examples](#-usage-examples) ยท [Contributing](#-contributing)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## โจ Why Error Shield?
|
|
23
|
+
|
|
24
|
+
| Pain Point | How Error Shield Helps |
|
|
25
|
+
| :------------------------------------------------ | :---------------------------------------------- |
|
|
26
|
+
| โ Inconsistent error responses across routes | โ
Uniform `ErrorDetails` structure everywhere |
|
|
27
|
+
| โ Boilerplate `try/catch` in every async handler | โ
`asyncHandler()` wraps it for you |
|
|
28
|
+
| โ Manually setting status codes & messages | โ
40+ pre-built `Errors` with correct codes |
|
|
29
|
+
| โ No context attached to errors for debugging | โ
`AppError` carries structured `context` data |
|
|
30
|
+
| โ Missing TypeScript types for errors | โ
Full type definitions included |
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## ๐ฆ Installation
|
|
15
35
|
|
|
16
36
|
```bash
|
|
37
|
+
# npm
|
|
17
38
|
npm install error-shield
|
|
39
|
+
|
|
40
|
+
# yarn
|
|
41
|
+
yarn add error-shield
|
|
42
|
+
|
|
43
|
+
# pnpm
|
|
44
|
+
pnpm add error-shield
|
|
18
45
|
```
|
|
19
46
|
|
|
20
|
-
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## โก Quick Start
|
|
21
50
|
|
|
22
|
-
|
|
51
|
+
Get up and running in **under 60 seconds**:
|
|
23
52
|
|
|
24
53
|
```javascript
|
|
25
|
-
const {
|
|
54
|
+
const {
|
|
55
|
+
AppError,
|
|
56
|
+
handleError,
|
|
57
|
+
Errors,
|
|
58
|
+
expressErrorHandler,
|
|
59
|
+
asyncHandler,
|
|
60
|
+
} = require("error-shield");
|
|
61
|
+
|
|
62
|
+
// 1๏ธโฃ Throw meaningful errors
|
|
63
|
+
throw Errors.notFound("User not found", { userId: 42 });
|
|
64
|
+
|
|
65
|
+
// 2๏ธโฃ Handle & format any error
|
|
66
|
+
const details = handleError(new Error("Oops"), { includeStack: true });
|
|
67
|
+
|
|
68
|
+
// 3๏ธโฃ Plug into Express with one line
|
|
69
|
+
app.use(expressErrorHandler({ includeTimestamp: true }));
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
That's it โ structured errors, formatted output, and Express integration with zero boilerplate. ๐
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## ๐ Table of Contents
|
|
77
|
+
|
|
78
|
+
- [Why Error Shield?](#-why-error-shield)
|
|
79
|
+
- [Installation](#-installation)
|
|
80
|
+
- [Quick Start](#-quick-start)
|
|
81
|
+
- [Usage Examples](#-usage-examples)
|
|
82
|
+
- [Custom Errors with `AppError`](#1%EF%B8%8Fโฃ-custom-errors-with-apperror)
|
|
83
|
+
- [Error Creators](#2%EF%B8%8Fโฃ-error-creators)
|
|
84
|
+
- [Express.js Middleware](#3%EF%B8%8Fโฃ-expressjs-middleware)
|
|
85
|
+
- [Async Handler Wrapper](#4%EF%B8%8Fโฃ-async-handler-wrapper)
|
|
86
|
+
- [Custom Logger](#5%EF%B8%8Fโฃ-custom-logger)
|
|
87
|
+
- [API Reference](#-api-reference)
|
|
88
|
+
- [Error Creators โ Full Reference](#-error-creators--full-reference)
|
|
89
|
+
- [TypeScript Support](#-typescript-support)
|
|
90
|
+
- [Contributing](#-contributing)
|
|
91
|
+
- [License](#-license)
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## ๐ Usage Examples
|
|
96
|
+
|
|
97
|
+
### 1๏ธโฃ Custom Errors with `AppError`
|
|
98
|
+
|
|
99
|
+
Create rich, structured errors with status codes, error codes, and arbitrary context:
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
const { AppError, handleError } = require("error-shield");
|
|
26
103
|
|
|
27
|
-
// Create a custom error
|
|
28
104
|
const error = new AppError("Something went wrong", 500, "CUSTOM_ERROR", {
|
|
29
105
|
userId: 123,
|
|
30
106
|
action: "updateProfile",
|
|
31
107
|
});
|
|
32
108
|
|
|
33
|
-
// Handle and format error
|
|
34
109
|
const errorDetails = handleError(error, {
|
|
35
110
|
includeStack: true,
|
|
36
111
|
includeTimestamp: true,
|
|
@@ -39,42 +114,79 @@ const errorDetails = handleError(error, {
|
|
|
39
114
|
console.log(errorDetails);
|
|
40
115
|
```
|
|
41
116
|
|
|
42
|
-
|
|
117
|
+
<details>
|
|
118
|
+
<summary>๐ <strong>Example Output</strong></summary>
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"message": "Something went wrong",
|
|
123
|
+
"statusCode": 500,
|
|
124
|
+
"code": "CUSTOM_ERROR",
|
|
125
|
+
"context": {
|
|
126
|
+
"userId": 123,
|
|
127
|
+
"action": "updateProfile"
|
|
128
|
+
},
|
|
129
|
+
"isOperational": true,
|
|
130
|
+
"timestamp": "2026-02-24T10:30:00.000Z",
|
|
131
|
+
"stack": "Error: Something went wrong\n at ..."
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
</details>
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### 2๏ธโฃ Error Creators
|
|
140
|
+
|
|
141
|
+
Use pre-built error factories for common HTTP errors โ no need to memorize status codes:
|
|
43
142
|
|
|
44
143
|
```javascript
|
|
45
|
-
const {
|
|
144
|
+
const { Errors } = require("error-shield");
|
|
46
145
|
|
|
47
|
-
// Bad Request
|
|
48
|
-
throw
|
|
146
|
+
// ๐ด 400 โ Bad Request
|
|
147
|
+
throw Errors.badRequest("Invalid input provided", { field: "email" });
|
|
49
148
|
|
|
50
|
-
//
|
|
51
|
-
throw
|
|
149
|
+
// ๐ 401 โ Unauthorized
|
|
150
|
+
throw Errors.unauthorized("Authentication required");
|
|
52
151
|
|
|
53
|
-
// Not Found
|
|
54
|
-
throw
|
|
152
|
+
// ๐ 404 โ Not Found
|
|
153
|
+
throw Errors.notFound("User not found", { userId: 123 });
|
|
55
154
|
|
|
56
|
-
// Validation Error
|
|
57
|
-
throw
|
|
155
|
+
// โ๏ธ 422 โ Validation Error
|
|
156
|
+
throw Errors.validationError("Email is required", { field: "email" });
|
|
157
|
+
|
|
158
|
+
// ๐ฆ 429 โ Too Many Requests
|
|
159
|
+
throw Errors.tooManyRequests("Rate limit exceeded", { retryAfter: 60 });
|
|
160
|
+
|
|
161
|
+
// ๐ฅ 500 โ Internal Server Error
|
|
162
|
+
throw Errors.internalServerError("Unexpected failure");
|
|
58
163
|
```
|
|
59
164
|
|
|
60
|
-
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
### 3๏ธโฃ Express.js Middleware
|
|
168
|
+
|
|
169
|
+
Plug in a production-ready error handler with a single line:
|
|
61
170
|
|
|
62
171
|
```javascript
|
|
63
172
|
const express = require("express");
|
|
64
|
-
const { expressErrorHandler,
|
|
173
|
+
const { expressErrorHandler, asyncHandler, Errors } = require("error-shield");
|
|
65
174
|
|
|
66
175
|
const app = express();
|
|
67
176
|
|
|
68
|
-
// Your routes
|
|
69
|
-
app.get(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
177
|
+
// Your routes โ errors are automatically caught & forwarded
|
|
178
|
+
app.get(
|
|
179
|
+
"/users/:id",
|
|
180
|
+
asyncHandler(async (req, res) => {
|
|
181
|
+
const user = await getUserById(req.params.id);
|
|
182
|
+
if (!user) {
|
|
183
|
+
throw Errors.notFound("User not found", { userId: req.params.id });
|
|
184
|
+
}
|
|
185
|
+
res.json(user);
|
|
186
|
+
}),
|
|
187
|
+
);
|
|
76
188
|
|
|
77
|
-
// Error handler middleware (must be last)
|
|
189
|
+
// โฌ๏ธ Error handler middleware (must be last)
|
|
78
190
|
app.use(
|
|
79
191
|
expressErrorHandler({
|
|
80
192
|
includeStack: process.env.NODE_ENV !== "production",
|
|
@@ -82,15 +194,31 @@ app.use(
|
|
|
82
194
|
}),
|
|
83
195
|
);
|
|
84
196
|
|
|
85
|
-
app.listen(3000);
|
|
197
|
+
app.listen(3000, () => console.log("๐ Server running on port 3000"));
|
|
86
198
|
```
|
|
87
199
|
|
|
88
|
-
|
|
200
|
+
> **๐ก Tip:** Combine `asyncHandler` with `expressErrorHandler` for completely boilerplate-free async route error handling.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
### 4๏ธโฃ Async Handler Wrapper
|
|
205
|
+
|
|
206
|
+
Eliminate `try/catch` blocks in your async route handlers:
|
|
89
207
|
|
|
90
208
|
```javascript
|
|
91
209
|
const { asyncHandler } = require("error-shield");
|
|
92
210
|
|
|
93
|
-
//
|
|
211
|
+
// โ Without asyncHandler
|
|
212
|
+
app.get("/api/data", async (req, res, next) => {
|
|
213
|
+
try {
|
|
214
|
+
const data = await fetchData();
|
|
215
|
+
res.json(data);
|
|
216
|
+
} catch (err) {
|
|
217
|
+
next(err); // easy to forget!
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// โ
With asyncHandler โ clean & safe
|
|
94
222
|
app.get(
|
|
95
223
|
"/api/data",
|
|
96
224
|
asyncHandler(async (req, res) => {
|
|
@@ -100,25 +228,31 @@ app.get(
|
|
|
100
228
|
);
|
|
101
229
|
```
|
|
102
230
|
|
|
103
|
-
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
### 5๏ธโฃ Custom Logger
|
|
234
|
+
|
|
235
|
+
Attach your own logging logic โ send errors to Sentry, Datadog, or any external service:
|
|
104
236
|
|
|
105
237
|
```javascript
|
|
106
238
|
const { handleError } = require("error-shield");
|
|
107
239
|
|
|
108
240
|
const errorDetails = handleError(error, {
|
|
109
|
-
logger: (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
241
|
+
logger: (details) => {
|
|
242
|
+
console.error("[ERROR]", details.message);
|
|
243
|
+
// ๐ค Send to your logging service
|
|
244
|
+
Sentry.captureException(details);
|
|
113
245
|
},
|
|
114
246
|
});
|
|
115
247
|
```
|
|
116
248
|
|
|
117
|
-
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
## ๐ API Reference
|
|
118
252
|
|
|
119
|
-
### `AppError`
|
|
253
|
+
### `AppError` class
|
|
120
254
|
|
|
121
|
-
|
|
255
|
+
> Extends the native `Error` class with structured metadata.
|
|
122
256
|
|
|
123
257
|
```typescript
|
|
124
258
|
class AppError extends Error {
|
|
@@ -129,127 +263,187 @@ class AppError extends Error {
|
|
|
129
263
|
}
|
|
130
264
|
```
|
|
131
265
|
|
|
132
|
-
|
|
266
|
+
| Parameter | Type | Default | Description |
|
|
267
|
+
| :----------- | :-------------------- | :-----: | :--------------------------- |
|
|
268
|
+
| `message` | `string` | โ | Error message |
|
|
269
|
+
| `statusCode` | `number` | `500` | HTTP status code |
|
|
270
|
+
| `code` | `string` | โ | Machine-readable error code |
|
|
271
|
+
| `context` | `Record<string, any>` | โ | Additional debugging context |
|
|
133
272
|
|
|
134
|
-
|
|
135
|
-
- `statusCode: number` - HTTP status code (default: 500)
|
|
136
|
-
- `code?: string` - Error code
|
|
137
|
-
- `context?: Record<string, any>` - Additional context
|
|
273
|
+
---
|
|
138
274
|
|
|
139
275
|
### `formatError(error, options?)`
|
|
140
276
|
|
|
141
|
-
Formats
|
|
142
|
-
|
|
143
|
-
**Parameters:**
|
|
277
|
+
> Formats any error into a consistent `ErrorDetails` object.
|
|
144
278
|
|
|
145
|
-
|
|
146
|
-
|
|
279
|
+
| Parameter | Type | Description |
|
|
280
|
+
| :-------- | :-------------------- | :------------------ |
|
|
281
|
+
| `error` | `Error \| AppError` | The error to format |
|
|
282
|
+
| `options` | `ErrorHandlerOptions` | Formatting options |
|
|
147
283
|
|
|
148
284
|
**Returns:** `ErrorDetails`
|
|
149
285
|
|
|
150
|
-
|
|
286
|
+
---
|
|
151
287
|
|
|
152
|
-
|
|
288
|
+
### `handleError(error, options?)`
|
|
153
289
|
|
|
154
|
-
|
|
290
|
+
> Handles errors with optional logging and formatting.
|
|
155
291
|
|
|
156
|
-
|
|
157
|
-
|
|
292
|
+
| Parameter | Type | Description |
|
|
293
|
+
| :-------- | :-------------------- | :---------------------------------------------------------------------- |
|
|
294
|
+
| `error` | `Error \| AppError` | The error to handle |
|
|
295
|
+
| `options` | `ErrorHandlerOptions` | Handler options (includes `logger`, `includeStack`, `includeTimestamp`) |
|
|
158
296
|
|
|
159
297
|
**Returns:** `ErrorDetails`
|
|
160
298
|
|
|
299
|
+
---
|
|
300
|
+
|
|
161
301
|
### `asyncHandler(fn)`
|
|
162
302
|
|
|
163
|
-
Wraps an async
|
|
303
|
+
> Wraps an async Express route handler to automatically catch rejected promises.
|
|
164
304
|
|
|
165
|
-
|
|
305
|
+
| Parameter | Type | Description |
|
|
306
|
+
| :-------- | :--------------------------------- | :--------------------------- |
|
|
307
|
+
| `fn` | `(req, res, next) => Promise<any>` | Async route handler function |
|
|
166
308
|
|
|
167
|
-
|
|
309
|
+
**Returns:** Wrapped Express middleware function
|
|
168
310
|
|
|
169
|
-
|
|
311
|
+
---
|
|
170
312
|
|
|
171
313
|
### `expressErrorHandler(options?)`
|
|
172
314
|
|
|
173
|
-
Creates an Express.js error
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
**Returns:** Express middleware
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
|
197
|
-
|
|
|
198
|
-
| `
|
|
199
|
-
| `
|
|
200
|
-
| `
|
|
201
|
-
| `
|
|
202
|
-
| `
|
|
203
|
-
| `
|
|
204
|
-
| `
|
|
205
|
-
| `
|
|
206
|
-
| `
|
|
207
|
-
| `
|
|
208
|
-
| `
|
|
209
|
-
| `
|
|
210
|
-
| `
|
|
211
|
-
| `
|
|
212
|
-
| `
|
|
213
|
-
| `
|
|
214
|
-
| `
|
|
215
|
-
| `
|
|
216
|
-
| `
|
|
217
|
-
| `
|
|
218
|
-
| `
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
|
223
|
-
|
|
|
224
|
-
| `
|
|
225
|
-
| `
|
|
226
|
-
| `
|
|
227
|
-
| `
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
|
235
|
-
|
|
|
236
|
-
| `
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
315
|
+
> Creates an Express.js error-handling middleware.
|
|
316
|
+
|
|
317
|
+
| Parameter | Type | Description |
|
|
318
|
+
| :-------- | :-------------------- | :-------------- |
|
|
319
|
+
| `options` | `ErrorHandlerOptions` | Handler options |
|
|
320
|
+
|
|
321
|
+
**Returns:** Express error middleware `(err, req, res, next) => void`
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
## ๐๏ธ Error Creators โ Full Reference
|
|
326
|
+
|
|
327
|
+
Pre-built factory methods for **all standard HTTP error codes**. Every method returns an `AppError` instance.
|
|
328
|
+
|
|
329
|
+
```javascript
|
|
330
|
+
// Signature for all creators:
|
|
331
|
+
Errors.methodName(message?, context?)
|
|
332
|
+
// โ Returns: AppError
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
<details>
|
|
336
|
+
<summary>๐ <strong>4xx Client Errors</strong> <em>(click to expand)</em></summary>
|
|
337
|
+
|
|
338
|
+
| Method | Code | Default Message |
|
|
339
|
+
| :------------------------------------------------ | :---: | :------------------------------ |
|
|
340
|
+
| `badRequest(message, context?)` | `400` | _(required)_ |
|
|
341
|
+
| `unauthorized(message?, context?)` | `401` | Unauthorized |
|
|
342
|
+
| `paymentRequired(message?, context?)` | `402` | Payment Required |
|
|
343
|
+
| `forbidden(message?, context?)` | `403` | Forbidden |
|
|
344
|
+
| `notFound(message?, context?)` | `404` | Not Found |
|
|
345
|
+
| `methodNotAllowed(message?, context?)` | `405` | Method Not Allowed |
|
|
346
|
+
| `notAcceptable(message?, context?)` | `406` | Not Acceptable |
|
|
347
|
+
| `proxyAuthRequired(message?, context?)` | `407` | Proxy Authentication Required |
|
|
348
|
+
| `requestTimeout(message?, context?)` | `408` | Request Timeout |
|
|
349
|
+
| `conflict(message, context?)` | `409` | _(required)_ |
|
|
350
|
+
| `gone(message?, context?)` | `410` | Gone |
|
|
351
|
+
| `lengthRequired(message?, context?)` | `411` | Length Required |
|
|
352
|
+
| `preconditionFailed(message?, context?)` | `412` | Precondition Failed |
|
|
353
|
+
| `payloadTooLarge(message?, context?)` | `413` | Payload Too Large |
|
|
354
|
+
| `uriTooLong(message?, context?)` | `414` | URI Too Long |
|
|
355
|
+
| `unsupportedMediaType(message?, context?)` | `415` | Unsupported Media Type |
|
|
356
|
+
| `rangeNotSatisfiable(message?, context?)` | `416` | Range Not Satisfiable |
|
|
357
|
+
| `expectationFailed(message?, context?)` | `417` | Expectation Failed |
|
|
358
|
+
| `imATeapot(message?, context?)` | `418` | I'm a Teapot |
|
|
359
|
+
| `misdirectedRequest(message?, context?)` | `421` | Misdirected Request |
|
|
360
|
+
| `unprocessableEntity(message?, context?)` | `422` | Unprocessable Entity |
|
|
361
|
+
| `validationError(message, context?)` | `422` | _(required)_ |
|
|
362
|
+
| `locked(message?, context?)` | `423` | Locked |
|
|
363
|
+
| `failedDependency(message?, context?)` | `424` | Failed Dependency |
|
|
364
|
+
| `tooEarly(message?, context?)` | `425` | Too Early |
|
|
365
|
+
| `upgradeRequired(message?, context?)` | `426` | Upgrade Required |
|
|
366
|
+
| `preconditionRequired(message?, context?)` | `428` | Precondition Required |
|
|
367
|
+
| `tooManyRequests(message?, context?)` | `429` | Too Many Requests |
|
|
368
|
+
| `requestHeaderFieldsTooLarge(message?, context?)` | `431` | Request Header Fields Too Large |
|
|
369
|
+
| `unavailableForLegalReasons(message?, context?)` | `451` | Unavailable For Legal Reasons |
|
|
370
|
+
|
|
371
|
+
</details>
|
|
372
|
+
|
|
373
|
+
<details>
|
|
374
|
+
<summary>๐ด <strong>5xx Server Errors</strong> <em>(click to expand)</em></summary>
|
|
375
|
+
|
|
376
|
+
| Method | Code | Default Message |
|
|
377
|
+
| :-------------------------------------------------- | :---: | :------------------------------ |
|
|
378
|
+
| `internalServerError(message?, context?)` | `500` | Internal Server Error |
|
|
379
|
+
| `notImplemented(message?, context?)` | `501` | Not Implemented |
|
|
380
|
+
| `badGateway(message?, context?)` | `502` | Bad Gateway |
|
|
381
|
+
| `serviceUnavailable(message?, context?)` | `503` | Service Unavailable |
|
|
382
|
+
| `gatewayTimeout(message?, context?)` | `504` | Gateway Timeout |
|
|
383
|
+
| `httpVersionNotSupported(message?, context?)` | `505` | HTTP Version Not Supported |
|
|
384
|
+
| `variantAlsoNegotiates(message?, context?)` | `506` | Variant Also Negotiates |
|
|
385
|
+
| `insufficientStorage(message?, context?)` | `507` | Insufficient Storage |
|
|
386
|
+
| `loopDetected(message?, context?)` | `508` | Loop Detected |
|
|
387
|
+
| `bandwidthLimitExceeded(message?, context?)` | `509` | Bandwidth Limit Exceeded |
|
|
388
|
+
| `notExtended(message?, context?)` | `510` | Not Extended |
|
|
389
|
+
| `networkAuthenticationRequired(message?, context?)` | `511` | Network Authentication Required |
|
|
390
|
+
| `networkConnectTimeout(message?, context?)` | `599` | Network Connect Timeout |
|
|
391
|
+
|
|
392
|
+
</details>
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
## ๐ฆ TypeScript Support
|
|
397
|
+
|
|
398
|
+
Error Shield ships with **full TypeScript declarations** โ zero extra config needed.
|
|
241
399
|
|
|
242
400
|
```typescript
|
|
243
|
-
import {
|
|
401
|
+
import {
|
|
402
|
+
AppError,
|
|
403
|
+
Errors,
|
|
404
|
+
handleError,
|
|
405
|
+
asyncHandler,
|
|
406
|
+
expressErrorHandler,
|
|
407
|
+
type ErrorDetails,
|
|
408
|
+
type ErrorHandlerOptions,
|
|
409
|
+
} from "error-shield";
|
|
410
|
+
|
|
411
|
+
// Fully typed error creation
|
|
412
|
+
const error: AppError = Errors.notFound("User not found", {
|
|
413
|
+
userId: 42,
|
|
414
|
+
});
|
|
244
415
|
|
|
245
|
-
|
|
246
|
-
const details = handleError(error
|
|
416
|
+
// Typed error details
|
|
417
|
+
const details: ErrorDetails = handleError(error, {
|
|
418
|
+
includeStack: true,
|
|
419
|
+
includeTimestamp: true,
|
|
420
|
+
});
|
|
247
421
|
```
|
|
248
422
|
|
|
249
|
-
|
|
423
|
+
---
|
|
424
|
+
|
|
425
|
+
## ๐ค Contributing
|
|
426
|
+
|
|
427
|
+
Contributions, issues, and feature requests are welcome!
|
|
428
|
+
|
|
429
|
+
1. **Fork** the repository
|
|
430
|
+
2. **Create** your feature branch โ `git checkout -b feature/amazing-feature`
|
|
431
|
+
3. **Commit** your changes โ `git commit -m "feat: add amazing feature"`
|
|
432
|
+
4. **Push** to the branch โ `git push origin feature/amazing-feature`
|
|
433
|
+
5. **Open** a Pull Request
|
|
434
|
+
|
|
435
|
+
---
|
|
436
|
+
|
|
437
|
+
## ๐ License
|
|
438
|
+
|
|
439
|
+
This project is licensed under the [ISC License](https://opensource.org/licenses/ISC).
|
|
440
|
+
|
|
441
|
+
---
|
|
442
|
+
|
|
443
|
+
<div align="center">
|
|
250
444
|
|
|
251
|
-
|
|
445
|
+
Made with โค๏ธ by **[Gopinath Kathirvel](https://github.com/Gopinathgopi13)**
|
|
252
446
|
|
|
253
|
-
|
|
447
|
+
โญ **If you found this useful, give it a star on [GitHub](https://github.com/Gopinathgopi13/error-shield)!** โญ
|
|
254
448
|
|
|
255
|
-
|
|
449
|
+
</div>
|