Skip to content

Commit 1150cae

Browse files
committed
fix: update tests and docs (#12)
1 parent 184bbac commit 1150cae

File tree

7 files changed

+138
-696
lines changed

7 files changed

+138
-696
lines changed

README.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
# handle-http-errors
22

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).
44

5-
## ☘️ Features
5+
## 🚂 Features
66

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
1111

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
2313

2414
### 🔧 Error Handler
2515

@@ -41,7 +31,7 @@ app.post('/users', async (req, res) => {
4131
});
4232
```
4333

44-
### 🌐 Middleware
34+
### 🌐 Express Middleware
4535

4636
```typescript
4737
import express from 'express';
@@ -60,17 +50,44 @@ app.get('/users/:id', (req, res, next) => {
6050
app.use(errorMiddleware());
6151
```
6252

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+
6380
## 🗂️ Error Classes
6481

6582
```typescript
6683
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
7491
} from 'handle-http-errors';
7592
```
7693

@@ -87,38 +104,21 @@ import {
87104
}
88105
```
89106

90-
## ⚙️ Configuration
107+
## 🪺 Examples
91108

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:
98110

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
104120
```
105121

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-
122122
## 📄 License
123123

124124
MIT

examples/README.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# handle-http-errors Examples
22

3-
Examples of using [handle-http-errors](https://www.npmjs.com/package/handle-http-errors) with different approaches.
3+
Examples of using [handle-http-errors](https://www.npmjs.com/package/handle-http-errors) package.
44

5-
## 🚀 Running Examples
5+
## 🚂 Run Examples
66

77
```bash
88
npm install
99

10-
npm run dev:handler # Error handler usage (Port 3001)
11-
npm run dev:middleware # Middleware usage (Port 3002)
12-
npm run dev:custom # Custom middlewares usage (Port 3003)
10+
npm run dev:handler # Error handler usage
11+
npm run dev:middleware # Express middleware usage
12+
npm run dev:custom-middleware # Creating custom error-throwing middlewares
1313
```
1414

15-
### 🔧 Error Handler Example (Port 3001)
15+
### 🔧 Error Handler Usage
1616

1717
```bash
1818
npm run dev:handler
1919
```
2020

21-
Test endpoints:
21+
#### 🌱 Error Handling Test Scenarios:
2222

2323
```bash
2424
# Test NotFoundError
@@ -43,13 +43,13 @@ curl -X POST \
4343
http://localhost:3001/products
4444
```
4545

46-
### 🌐 Middleware Example (Port 3002)
46+
### 🌐 Express Middleware Usage
4747

4848
```bash
4949
npm run dev:middleware
5050
```
5151

52-
Test endpoints:
52+
#### 🌱 Error Handling Test Scenarios:
5353

5454
```bash
5555
# Test NotFoundError
@@ -73,7 +73,7 @@ curl -X POST \
7373
-d '{"email":"invalid-email","username":"test"}' \
7474
http://localhost:3002/users/register
7575

76-
# Test ValidationError with short username
76+
# Test ValidationError with invalid username
7777
curl -X POST \
7878
-H "Content-Type: application/json" \
7979
-d '{"email":"[email protected]","username":"a"}' \
@@ -86,13 +86,13 @@ curl -X POST \
8686
http://localhost:3002/users/register
8787
```
8888

89-
### 🛠️ Custom Middleware Example (Port 3003)
89+
### 🛠️ Creating Custom Error-throwing Middlewares
9090

9191
```bash
9292
npm run dev:custom-middleware
9393
```
9494

95-
Test endpoints:
95+
#### 🌱 Error Handling Test Scenarios:
9696

9797
```bash
9898
# Test UnauthorizedError without token
@@ -123,7 +123,7 @@ curl -X POST \
123123
-d '{"email":"invalid-email","username":"a"}' \
124124
http://localhost:3003/users/register
125125

126-
# Test ForbiddenError when creating admin
126+
# Test ForbiddenError when creating admin users
127127
curl -X POST \
128128
-H "Authorization: valid-token" \
129129
-H "Content-Type: application/json" \
@@ -137,8 +137,3 @@ curl -X POST \
137137
-d '{"email":"[email protected]","username":"testuser"}' \
138138
http://localhost:3003/users/register
139139
```
140-
141-
Each example demonstrates different aspects of error handling:
142-
- Error Handler: Direct usage of errorHandler
143-
- Middleware: Global error handling with middleware
144-
- Custom Middleware: Creating custom error-throwing middlewares

0 commit comments

Comments
 (0)