error-shield 1.1.0 โ 1.2.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/LICENSE +15 -0
- package/README.md +334 -136
- package/package.json +1 -1
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 `ErrorCreators` 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
|
+
ErrorCreators,
|
|
58
|
+
expressErrorHandler,
|
|
59
|
+
asyncHandler,
|
|
60
|
+
} = require("error-shield");
|
|
61
|
+
|
|
62
|
+
// 1๏ธโฃ Throw meaningful errors
|
|
63
|
+
throw ErrorCreators.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,83 @@ 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
144
|
const { ErrorCreators } = require("error-shield");
|
|
46
145
|
|
|
47
|
-
// Bad Request
|
|
146
|
+
// ๐ด 400 โ Bad Request
|
|
48
147
|
throw ErrorCreators.badRequest("Invalid input provided", { field: "email" });
|
|
49
148
|
|
|
50
|
-
//
|
|
149
|
+
// ๐ 401 โ Unauthorized
|
|
51
150
|
throw ErrorCreators.unauthorized("Authentication required");
|
|
52
151
|
|
|
53
|
-
// Not Found
|
|
152
|
+
// ๐ 404 โ Not Found
|
|
54
153
|
throw ErrorCreators.notFound("User not found", { userId: 123 });
|
|
55
154
|
|
|
56
|
-
// Validation Error
|
|
155
|
+
// โ๏ธ 422 โ Validation Error
|
|
57
156
|
throw ErrorCreators.validationError("Email is required", { field: "email" });
|
|
157
|
+
|
|
158
|
+
// ๐ฆ 429 โ Too Many Requests
|
|
159
|
+
throw ErrorCreators.tooManyRequests("Rate limit exceeded", { retryAfter: 60 });
|
|
160
|
+
|
|
161
|
+
// ๐ฅ 500 โ Internal Server Error
|
|
162
|
+
throw ErrorCreators.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 {
|
|
173
|
+
const {
|
|
174
|
+
expressErrorHandler,
|
|
175
|
+
asyncHandler,
|
|
176
|
+
ErrorCreators,
|
|
177
|
+
} = require("error-shield");
|
|
65
178
|
|
|
66
179
|
const app = express();
|
|
67
180
|
|
|
68
|
-
// Your routes
|
|
69
|
-
app.get(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
181
|
+
// Your routes โ errors are automatically caught & forwarded
|
|
182
|
+
app.get(
|
|
183
|
+
"/users/:id",
|
|
184
|
+
asyncHandler(async (req, res) => {
|
|
185
|
+
const user = await getUserById(req.params.id);
|
|
186
|
+
if (!user) {
|
|
187
|
+
throw ErrorCreators.notFound("User not found", { userId: req.params.id });
|
|
188
|
+
}
|
|
189
|
+
res.json(user);
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
76
192
|
|
|
77
|
-
// Error handler middleware (must be last)
|
|
193
|
+
// โฌ๏ธ Error handler middleware (must be last)
|
|
78
194
|
app.use(
|
|
79
195
|
expressErrorHandler({
|
|
80
196
|
includeStack: process.env.NODE_ENV !== "production",
|
|
@@ -82,15 +198,31 @@ app.use(
|
|
|
82
198
|
}),
|
|
83
199
|
);
|
|
84
200
|
|
|
85
|
-
app.listen(3000);
|
|
201
|
+
app.listen(3000, () => console.log("๐ Server running on port 3000"));
|
|
86
202
|
```
|
|
87
203
|
|
|
88
|
-
|
|
204
|
+
> **๐ก Tip:** Combine `asyncHandler` with `expressErrorHandler` for completely boilerplate-free async route error handling.
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
### 4๏ธโฃ Async Handler Wrapper
|
|
209
|
+
|
|
210
|
+
Eliminate `try/catch` blocks in your async route handlers:
|
|
89
211
|
|
|
90
212
|
```javascript
|
|
91
213
|
const { asyncHandler } = require("error-shield");
|
|
92
214
|
|
|
93
|
-
//
|
|
215
|
+
// โ Without asyncHandler
|
|
216
|
+
app.get("/api/data", async (req, res, next) => {
|
|
217
|
+
try {
|
|
218
|
+
const data = await fetchData();
|
|
219
|
+
res.json(data);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
next(err); // easy to forget!
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// โ
With asyncHandler โ clean & safe
|
|
94
226
|
app.get(
|
|
95
227
|
"/api/data",
|
|
96
228
|
asyncHandler(async (req, res) => {
|
|
@@ -100,25 +232,31 @@ app.get(
|
|
|
100
232
|
);
|
|
101
233
|
```
|
|
102
234
|
|
|
103
|
-
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
### 5๏ธโฃ Custom Logger
|
|
238
|
+
|
|
239
|
+
Attach your own logging logic โ send errors to Sentry, Datadog, or any external service:
|
|
104
240
|
|
|
105
241
|
```javascript
|
|
106
242
|
const { handleError } = require("error-shield");
|
|
107
243
|
|
|
108
244
|
const errorDetails = handleError(error, {
|
|
109
|
-
logger: (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
245
|
+
logger: (details) => {
|
|
246
|
+
console.error("[ERROR]", details.message);
|
|
247
|
+
// ๐ค Send to your logging service
|
|
248
|
+
Sentry.captureException(details);
|
|
113
249
|
},
|
|
114
250
|
});
|
|
115
251
|
```
|
|
116
252
|
|
|
117
|
-
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## ๐ API Reference
|
|
118
256
|
|
|
119
|
-
### `AppError`
|
|
257
|
+
### `AppError` class
|
|
120
258
|
|
|
121
|
-
|
|
259
|
+
> Extends the native `Error` class with structured metadata.
|
|
122
260
|
|
|
123
261
|
```typescript
|
|
124
262
|
class AppError extends Error {
|
|
@@ -129,127 +267,187 @@ class AppError extends Error {
|
|
|
129
267
|
}
|
|
130
268
|
```
|
|
131
269
|
|
|
132
|
-
|
|
270
|
+
| Parameter | Type | Default | Description |
|
|
271
|
+
| :----------- | :-------------------- | :-----: | :--------------------------- |
|
|
272
|
+
| `message` | `string` | โ | Error message |
|
|
273
|
+
| `statusCode` | `number` | `500` | HTTP status code |
|
|
274
|
+
| `code` | `string` | โ | Machine-readable error code |
|
|
275
|
+
| `context` | `Record<string, any>` | โ | Additional debugging context |
|
|
133
276
|
|
|
134
|
-
|
|
135
|
-
- `statusCode: number` - HTTP status code (default: 500)
|
|
136
|
-
- `code?: string` - Error code
|
|
137
|
-
- `context?: Record<string, any>` - Additional context
|
|
277
|
+
---
|
|
138
278
|
|
|
139
279
|
### `formatError(error, options?)`
|
|
140
280
|
|
|
141
|
-
Formats
|
|
142
|
-
|
|
143
|
-
**Parameters:**
|
|
281
|
+
> Formats any error into a consistent `ErrorDetails` object.
|
|
144
282
|
|
|
145
|
-
|
|
146
|
-
|
|
283
|
+
| Parameter | Type | Description |
|
|
284
|
+
| :-------- | :-------------------- | :------------------ |
|
|
285
|
+
| `error` | `Error \| AppError` | The error to format |
|
|
286
|
+
| `options` | `ErrorHandlerOptions` | Formatting options |
|
|
147
287
|
|
|
148
288
|
**Returns:** `ErrorDetails`
|
|
149
289
|
|
|
150
|
-
|
|
290
|
+
---
|
|
151
291
|
|
|
152
|
-
|
|
292
|
+
### `handleError(error, options?)`
|
|
153
293
|
|
|
154
|
-
|
|
294
|
+
> Handles errors with optional logging and formatting.
|
|
155
295
|
|
|
156
|
-
|
|
157
|
-
|
|
296
|
+
| Parameter | Type | Description |
|
|
297
|
+
| :-------- | :-------------------- | :---------------------------------------------------------------------- |
|
|
298
|
+
| `error` | `Error \| AppError` | The error to handle |
|
|
299
|
+
| `options` | `ErrorHandlerOptions` | Handler options (includes `logger`, `includeStack`, `includeTimestamp`) |
|
|
158
300
|
|
|
159
301
|
**Returns:** `ErrorDetails`
|
|
160
302
|
|
|
303
|
+
---
|
|
304
|
+
|
|
161
305
|
### `asyncHandler(fn)`
|
|
162
306
|
|
|
163
|
-
Wraps an async
|
|
307
|
+
> Wraps an async Express route handler to automatically catch rejected promises.
|
|
164
308
|
|
|
165
|
-
|
|
309
|
+
| Parameter | Type | Description |
|
|
310
|
+
| :-------- | :--------------------------------- | :--------------------------- |
|
|
311
|
+
| `fn` | `(req, res, next) => Promise<any>` | Async route handler function |
|
|
166
312
|
|
|
167
|
-
|
|
313
|
+
**Returns:** Wrapped Express middleware function
|
|
168
314
|
|
|
169
|
-
|
|
315
|
+
---
|
|
170
316
|
|
|
171
317
|
### `expressErrorHandler(options?)`
|
|
172
318
|
|
|
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
|
-
|
|
319
|
+
> Creates an Express.js error-handling middleware.
|
|
320
|
+
|
|
321
|
+
| Parameter | Type | Description |
|
|
322
|
+
| :-------- | :-------------------- | :-------------- |
|
|
323
|
+
| `options` | `ErrorHandlerOptions` | Handler options |
|
|
324
|
+
|
|
325
|
+
**Returns:** Express error middleware `(err, req, res, next) => void`
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## ๐๏ธ Error Creators โ Full Reference
|
|
330
|
+
|
|
331
|
+
Pre-built factory methods for **all standard HTTP error codes**. Every method returns an `AppError` instance.
|
|
332
|
+
|
|
333
|
+
```javascript
|
|
334
|
+
// Signature for all creators:
|
|
335
|
+
ErrorCreators.methodName(message?, context?)
|
|
336
|
+
// โ Returns: AppError
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
<details>
|
|
340
|
+
<summary>๐ <strong>4xx Client Errors</strong> <em>(click to expand)</em></summary>
|
|
341
|
+
|
|
342
|
+
| Method | Code | Default Message |
|
|
343
|
+
| :------------------------------------------------ | :---: | :------------------------------ |
|
|
344
|
+
| `badRequest(message, context?)` | `400` | _(required)_ |
|
|
345
|
+
| `unauthorized(message?, context?)` | `401` | Unauthorized |
|
|
346
|
+
| `paymentRequired(message?, context?)` | `402` | Payment Required |
|
|
347
|
+
| `forbidden(message?, context?)` | `403` | Forbidden |
|
|
348
|
+
| `notFound(message?, context?)` | `404` | Not Found |
|
|
349
|
+
| `methodNotAllowed(message?, context?)` | `405` | Method Not Allowed |
|
|
350
|
+
| `notAcceptable(message?, context?)` | `406` | Not Acceptable |
|
|
351
|
+
| `proxyAuthRequired(message?, context?)` | `407` | Proxy Authentication Required |
|
|
352
|
+
| `requestTimeout(message?, context?)` | `408` | Request Timeout |
|
|
353
|
+
| `conflict(message, context?)` | `409` | _(required)_ |
|
|
354
|
+
| `gone(message?, context?)` | `410` | Gone |
|
|
355
|
+
| `lengthRequired(message?, context?)` | `411` | Length Required |
|
|
356
|
+
| `preconditionFailed(message?, context?)` | `412` | Precondition Failed |
|
|
357
|
+
| `payloadTooLarge(message?, context?)` | `413` | Payload Too Large |
|
|
358
|
+
| `uriTooLong(message?, context?)` | `414` | URI Too Long |
|
|
359
|
+
| `unsupportedMediaType(message?, context?)` | `415` | Unsupported Media Type |
|
|
360
|
+
| `rangeNotSatisfiable(message?, context?)` | `416` | Range Not Satisfiable |
|
|
361
|
+
| `expectationFailed(message?, context?)` | `417` | Expectation Failed |
|
|
362
|
+
| `imATeapot(message?, context?)` | `418` | I'm a Teapot |
|
|
363
|
+
| `misdirectedRequest(message?, context?)` | `421` | Misdirected Request |
|
|
364
|
+
| `unprocessableEntity(message?, context?)` | `422` | Unprocessable Entity |
|
|
365
|
+
| `validationError(message, context?)` | `422` | _(required)_ |
|
|
366
|
+
| `locked(message?, context?)` | `423` | Locked |
|
|
367
|
+
| `failedDependency(message?, context?)` | `424` | Failed Dependency |
|
|
368
|
+
| `tooEarly(message?, context?)` | `425` | Too Early |
|
|
369
|
+
| `upgradeRequired(message?, context?)` | `426` | Upgrade Required |
|
|
370
|
+
| `preconditionRequired(message?, context?)` | `428` | Precondition Required |
|
|
371
|
+
| `tooManyRequests(message?, context?)` | `429` | Too Many Requests |
|
|
372
|
+
| `requestHeaderFieldsTooLarge(message?, context?)` | `431` | Request Header Fields Too Large |
|
|
373
|
+
| `unavailableForLegalReasons(message?, context?)` | `451` | Unavailable For Legal Reasons |
|
|
374
|
+
|
|
375
|
+
</details>
|
|
376
|
+
|
|
377
|
+
<details>
|
|
378
|
+
<summary>๐ด <strong>5xx Server Errors</strong> <em>(click to expand)</em></summary>
|
|
379
|
+
|
|
380
|
+
| Method | Code | Default Message |
|
|
381
|
+
| :-------------------------------------------------- | :---: | :------------------------------ |
|
|
382
|
+
| `internalServerError(message?, context?)` | `500` | Internal Server Error |
|
|
383
|
+
| `notImplemented(message?, context?)` | `501` | Not Implemented |
|
|
384
|
+
| `badGateway(message?, context?)` | `502` | Bad Gateway |
|
|
385
|
+
| `serviceUnavailable(message?, context?)` | `503` | Service Unavailable |
|
|
386
|
+
| `gatewayTimeout(message?, context?)` | `504` | Gateway Timeout |
|
|
387
|
+
| `httpVersionNotSupported(message?, context?)` | `505` | HTTP Version Not Supported |
|
|
388
|
+
| `variantAlsoNegotiates(message?, context?)` | `506` | Variant Also Negotiates |
|
|
389
|
+
| `insufficientStorage(message?, context?)` | `507` | Insufficient Storage |
|
|
390
|
+
| `loopDetected(message?, context?)` | `508` | Loop Detected |
|
|
391
|
+
| `bandwidthLimitExceeded(message?, context?)` | `509` | Bandwidth Limit Exceeded |
|
|
392
|
+
| `notExtended(message?, context?)` | `510` | Not Extended |
|
|
393
|
+
| `networkAuthenticationRequired(message?, context?)` | `511` | Network Authentication Required |
|
|
394
|
+
| `networkConnectTimeout(message?, context?)` | `599` | Network Connect Timeout |
|
|
395
|
+
|
|
396
|
+
</details>
|
|
397
|
+
|
|
398
|
+
---
|
|
399
|
+
|
|
400
|
+
## ๐ฆ TypeScript Support
|
|
401
|
+
|
|
402
|
+
Error Shield ships with **full TypeScript declarations** โ zero extra config needed.
|
|
241
403
|
|
|
242
404
|
```typescript
|
|
243
|
-
import {
|
|
405
|
+
import {
|
|
406
|
+
AppError,
|
|
407
|
+
ErrorCreators,
|
|
408
|
+
handleError,
|
|
409
|
+
asyncHandler,
|
|
410
|
+
expressErrorHandler,
|
|
411
|
+
type ErrorDetails,
|
|
412
|
+
type ErrorHandlerOptions,
|
|
413
|
+
} from "error-shield";
|
|
414
|
+
|
|
415
|
+
// Fully typed error creation
|
|
416
|
+
const error: AppError = ErrorCreators.notFound("User not found", {
|
|
417
|
+
userId: 42,
|
|
418
|
+
});
|
|
244
419
|
|
|
245
|
-
|
|
246
|
-
const details = handleError(error
|
|
420
|
+
// Typed error details
|
|
421
|
+
const details: ErrorDetails = handleError(error, {
|
|
422
|
+
includeStack: true,
|
|
423
|
+
includeTimestamp: true,
|
|
424
|
+
});
|
|
247
425
|
```
|
|
248
426
|
|
|
249
|
-
|
|
427
|
+
---
|
|
428
|
+
|
|
429
|
+
## ๐ค Contributing
|
|
430
|
+
|
|
431
|
+
Contributions, issues, and feature requests are welcome!
|
|
432
|
+
|
|
433
|
+
1. **Fork** the repository
|
|
434
|
+
2. **Create** your feature branch โ `git checkout -b feature/amazing-feature`
|
|
435
|
+
3. **Commit** your changes โ `git commit -m "feat: add amazing feature"`
|
|
436
|
+
4. **Push** to the branch โ `git push origin feature/amazing-feature`
|
|
437
|
+
5. **Open** a Pull Request
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
## ๐ License
|
|
442
|
+
|
|
443
|
+
This project is licensed under the [ISC License](https://opensource.org/licenses/ISC).
|
|
444
|
+
|
|
445
|
+
---
|
|
446
|
+
|
|
447
|
+
<div align="center">
|
|
250
448
|
|
|
251
|
-
|
|
449
|
+
Made with โค๏ธ by **[Gopinath Kathirvel](https://github.com/Gopinathgopi13)**
|
|
252
450
|
|
|
253
|
-
|
|
451
|
+
โญ **If you found this useful, give it a star on [GitHub](https://github.com/Gopinathgopi13/error-shield)!** โญ
|
|
254
452
|
|
|
255
|
-
|
|
453
|
+
</div>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "error-shield",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Comprehensive error handling utility for Node.js & Express.js โ custom error classes, async handler wrapper, Express middleware, HTTP error creators, and TypeScript support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"error-handling",
|