1
1
# handle-http-errors
2
2
3
- HTTP error handling library with TypeScript support, providing error classes, handlers, and middleware support.
3
+ Type-safe HTTP error handling package providing error classes, standardized responses, error handler, and built-in Express middleware support. Available as [ NPM package ] ( https://www.npmjs.com/package/handle-http-errors ) .
4
4
5
- ## ☘️ Features
5
+ ## 🚂 Features
6
6
7
- - Error Classes - Built-in HTTP error classes with type support
8
- - Error Handler - Flexible error handling with standardized responses
9
- - Middleware Support - Ready-to-use Express middleware
10
- - TypeScript Support - Full type safety with TypeScript
7
+ - Error Classes
8
+ - Error Handler
9
+ - Express Middleware Support
10
+ - TypeScript Support
11
11
12
- ## 📥 Installation
13
-
14
- ``` bash
15
- npm install handle-http-errors
16
- # or
17
- yarn add handle-http-errors
18
- # or
19
- pnpm add handle-http-errors
20
- ```
21
-
22
- ## 📖 Usage
12
+ ## ☘️ Usage
23
13
24
14
### 🔧 Error Handler
25
15
@@ -41,7 +31,7 @@ app.post('/users', async (req, res) => {
41
31
});
42
32
```
43
33
44
- ### 🌐 Middleware
34
+ ### 🌐 Express Middleware
45
35
46
36
``` typescript
47
37
import express from ' express' ;
@@ -60,17 +50,44 @@ app.get('/users/:id', (req, res, next) => {
60
50
app .use (errorMiddleware ());
61
51
```
62
52
53
+ ## ⚙️ Configuration
54
+
55
+ ``` typescript
56
+ interface ErrorHandlerOptions {
57
+ includeStack? : boolean ; // Include stack traces
58
+ onError? : (error : unknown ) => void ; // onError callback
59
+ }
60
+
61
+ // Use with handler
62
+ app .post (' /users' , async (req , res ) => {
63
+ try {
64
+ throw new ValidationError (' Invalid data' );
65
+ } catch (error ) {
66
+ return errorHandler (error , res , {
67
+ includeStack: process .env .NODE_ENV !== ' production' ,
68
+ onError : (error ) => console .error (error )
69
+ });
70
+ }
71
+ });
72
+
73
+ // Use with middleware
74
+ app .use (errorMiddleware ({
75
+ includeStack: process .env .NODE_ENV !== ' production' ,
76
+ onError : (error ) => console .error (error )
77
+ }));
78
+ ```
79
+
63
80
## 🗂️ Error Classes
64
81
65
82
``` typescript
66
83
import {
67
- HttpError , // Base error class
68
- ValidationError , // 400 - Validation errors
69
- BadRequestError , // 400 - Malformed requests
70
- UnauthorizedError , // 401 - Authentication errors
71
- ForbiddenError , // 403 - Authorization errors
72
- NotFoundError , // 404 - Resource not found
73
- InternalServerError // 500 - Server errors
84
+ HttpError , // Base error class
85
+ ValidationError , // 400 - Validation errors
86
+ BadRequestError , // 400 - Malformed requests
87
+ UnauthorizedError , // 401 - Authentication errors
88
+ ForbiddenError , // 403 - Authorization errors
89
+ NotFoundError , // 404 - Resource not found
90
+ InternalServerError // 500 - Server errors
74
91
} from ' handle-http-errors' ;
75
92
```
76
93
@@ -87,38 +104,21 @@ import {
87
104
}
88
105
```
89
106
90
- ## ⚙️ Configuration
107
+ ## 🪺 Examples
91
108
92
- ``` typescript
93
- // Middleware options
94
- interface ErrorHandlerOptions {
95
- includeStack? : boolean ; // Include stack traces
96
- onError? : (error : unknown ) => void ; // Error callback
97
- }
109
+ Check out the [ examples] ( https://github.com/junjie-w/handle-http-errors/tree/main/examples ) directory for detailed usage examples:
98
110
99
- // Using with options
100
- app .use (errorMiddleware ({
101
- includeStack: process .env .NODE_ENV !== ' production' ,
102
- onError : (error ) => console .error (error )
103
- }));
111
+ ``` bash
112
+ git clone https://github.com/junjie-w/handle-http-errors.git
113
+ cd handle-http-errors/examples
114
+ npm install
115
+
116
+ # Try different examples
117
+ npm run dev:handler # Error handler usage
118
+ npm run dev:middleware # Express middleware usage
119
+ npm run dev:custom-middleware # Creating custom error-throwing middlewares
104
120
```
105
121
106
- ## 🔍 Development vs Production
107
-
108
- Development Mode (` NODE_ENV !== 'production' ` ):
109
- - Detailed error messages
110
- - Stack traces (when enabled)
111
- - Error details included
112
-
113
- Production Mode (` NODE_ENV === 'production' ` ):
114
- - Generic error messages
115
- - No stack traces
116
- - Limited error details
117
-
118
- ## 📚 Examples
119
-
120
- Check out the [ examples] ( https://github.com/junjie-w/handle-http-errors/tree/main/examples ) directory for detailed usage examples.
121
-
122
122
## 📄 License
123
123
124
124
MIT
0 commit comments