Skip to content

Commit dd4c8eb

Browse files
authored
Merge pull request #9 from junjie-w/develop
Merge Develop into Main
2 parents 6dfdc3e + d00b492 commit dd4c8eb

34 files changed

+16249
-0
lines changed

.github/workflows/cd.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Release and Publish
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Quality Checks"]
6+
types:
7+
- completed
8+
branches: [main]
9+
workflow_dispatch:
10+
11+
jobs:
12+
release-and-publish:
13+
name: Release and Publish
14+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
packages: write
19+
issues: write
20+
pull-requests: write
21+
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0
27+
token: ${{ secrets.GITHUB_TOKEN }}
28+
29+
- name: Setup Node.js
30+
uses: actions/setup-node@v4
31+
with:
32+
node-version: 20
33+
cache: npm
34+
registry-url: "https://registry.npmjs.org"
35+
always-auth: true
36+
token: ${{ secrets.NPM_TOKEN }}
37+
38+
- name: Install dependencies
39+
run: npm ci
40+
41+
- name: Build Package
42+
run: npm run build
43+
44+
- name: Release
45+
env:
46+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
48+
run: npx semantic-release

.github/workflows/ci.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Quality Checks
2+
3+
on:
4+
pull_request:
5+
branches: [main, develop]
6+
push:
7+
branches: [main]
8+
9+
jobs:
10+
test-and-lint:
11+
name: Test and Lint
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: 20
22+
cache: 'npm'
23+
24+
- name: Install dependencies
25+
run: npm ci
26+
27+
- name: Run linting
28+
run: npm run lint
29+
30+
- name: Build package
31+
run: npm run build
32+
33+
- name: Run tests with coverage
34+
run: npm run test:coverage
35+
36+
- name: Upload coverage report
37+
if: success()
38+
uses: actions/upload-artifact@v4
39+
with:
40+
name: coverage-report
41+
path: coverage/
42+
if-no-files-found: error
43+
44+
verify-release:
45+
name: Verify Release
46+
runs-on: ubuntu-latest
47+
permissions:
48+
contents: write
49+
issues: write
50+
pull-requests: write
51+
52+
steps:
53+
- name: Checkout code
54+
uses: actions/checkout@v4
55+
56+
- name: Set up Node.js
57+
uses: actions/setup-node@v4
58+
with:
59+
node-version: 20
60+
cache: 'npm'
61+
62+
- name: Install dependencies
63+
run: npm ci
64+
65+
- name: Build package
66+
run: npm run build
67+
68+
- name: Semantic-release dry-run
69+
env:
70+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
72+
run: npx semantic-release --dry-run

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Node.js
2+
node_modules/
3+
dist/
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# TypeScript
9+
*.tsbuildinfo
10+
11+
# Logs
12+
logs
13+
*.log
14+
logs/*.log
15+
16+
# Dependency directories
17+
jspm_packages/
18+
19+
# Optional npm cache directory
20+
.npm
21+
22+
# Optional eslint cache
23+
.eslintcache
24+
25+
# Optional REPL history
26+
.node_repl_history
27+
28+
# Output of 'npm pack'
29+
*.tgz
30+
31+
# dotenv environment variables file
32+
.env
33+
34+
# Mac system files
35+
.DS_Store
36+
37+
# Coverage directory
38+
coverage/

.husky/commit-msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx --no -- commitlint --edit ${1}

.husky/pre-commit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
npm audit --omit=dev || (
2+
echo '🚨 npm audit found vulnerabilities!'
3+
echo 'Review the audit report above and fix any high-severity issues.'
4+
exit 1
5+
)
6+
7+
npx lint-staged

.lintstagedrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
"(src|tests|scripts|config|.)/**/*.{js,ts,json,yml}": [
3+
"eslint --cache --fix",
4+
"jest --bail --passWithNoTests --findRelatedTests",
5+
],
6+
};

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
examples/
2+
tests/
3+
__tests__/
4+
src/
5+
tsconfig.json
6+
.gitignore
7+
.npmignore

.releaserc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
branches: ["main"],
3+
plugins: [
4+
"@semantic-release/commit-analyzer",
5+
"@semantic-release/release-notes-generator",
6+
"@semantic-release/changelog",
7+
"@semantic-release/npm",
8+
"@semantic-release/github",
9+
[
10+
"@semantic-release/git",
11+
{
12+
assets: ["package.json", "package-lock.json", "CHANGELOG.md"],
13+
message:
14+
"chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}",
15+
},
16+
],
17+
],
18+
};

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Junjie Wu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# handle-http-errors
2+
3+
HTTP error handling library with TypeScript support, providing error classes, handlers, and middleware support.
4+
5+
## ☘️ Features
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
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
23+
24+
### 🔧 Error Handler
25+
26+
```typescript
27+
import express from 'express';
28+
import { errorHandler, ValidationError } from 'handle-http-errors';
29+
30+
const app = express();
31+
32+
app.post('/users', async (req, res) => {
33+
try {
34+
const { email } = req.body;
35+
if (!email) {
36+
throw new ValidationError('Email is required');
37+
}
38+
} catch (error) {
39+
return errorHandler(error, res);
40+
}
41+
});
42+
```
43+
44+
### 🌐 Middleware
45+
46+
```typescript
47+
import express from 'express';
48+
import { errorMiddleware, NotFoundError } from 'handle-http-errors';
49+
50+
const app = express();
51+
52+
app.get('/users/:id', (req, res, next) => {
53+
try {
54+
throw new NotFoundError('User not found', { id: req.params.id });
55+
} catch (error) {
56+
next(error);
57+
}
58+
});
59+
60+
app.use(errorMiddleware());
61+
```
62+
63+
## 🗂️ Error Classes
64+
65+
```typescript
66+
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
74+
} from 'handle-http-errors';
75+
```
76+
77+
## 📋 Error Response Format
78+
79+
```typescript
80+
{
81+
status: number; // HTTP status code
82+
code: string; // Error code (e.g., 'VALIDATION_ERROR')
83+
message: string; // Error message
84+
timestamp: string; // ISO timestamp
85+
details?: object; // Optional error details
86+
stack?: string; // Stack trace (development only)
87+
}
88+
```
89+
90+
## ⚙️ Configuration
91+
92+
```typescript
93+
// Middleware options
94+
interface ErrorHandlerOptions {
95+
includeStack?: boolean; // Include stack traces
96+
onError?: (error: unknown) => void; // Error callback
97+
}
98+
99+
// Using with options
100+
app.use(errorMiddleware({
101+
includeStack: process.env.NODE_ENV !== 'production',
102+
onError: (error) => console.error(error)
103+
}));
104+
```
105+
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+
## 📄 License
123+
124+
MIT

0 commit comments

Comments
 (0)