@erangamadhushan/express-advanced-error-kit 1.0.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/README.md +79 -48
- package/dist/index.cjs +7 -2
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +7 -2
- package/package.json +15 -2
package/README.md
CHANGED
|
@@ -1,48 +1,79 @@
|
|
|
1
|
-
# express-advanced-error-kit
|
|
2
|
-
|
|
3
|
-
Advanced TypeScript-based error handling middleware for Express.js.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
##
|
|
24
|
-
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
# @erangamadhushan/express-advanced-error-kit
|
|
2
|
+
|
|
3
|
+
Advanced TypeScript-based error handling middleware for Express.js.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+

|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Async handler wrapper
|
|
14
|
+
- Custom ApiError class
|
|
15
|
+
- Global error middleware
|
|
16
|
+
- 404 not found middleware
|
|
17
|
+
- Production-safe stack hiding
|
|
18
|
+
- ESM + CommonJS support
|
|
19
|
+
- Full TypeScript definitions
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @erangamadhushan/express-advanced-error-kit
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import express from "express";
|
|
33
|
+
import {
|
|
34
|
+
asyncHandler,
|
|
35
|
+
ApiError,
|
|
36
|
+
notFoundMiddleware,
|
|
37
|
+
errorMiddleware
|
|
38
|
+
} from "@erangamadhushan/express-advanced-error-kit";
|
|
39
|
+
|
|
40
|
+
const app = express();
|
|
41
|
+
|
|
42
|
+
app.get(
|
|
43
|
+
"/error",
|
|
44
|
+
asyncHandler(async () => {
|
|
45
|
+
throw new ApiError("Something went wrong", 400);
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
app.use(notFoundMiddleware);
|
|
50
|
+
app.use(errorMiddleware);
|
|
51
|
+
|
|
52
|
+
app.listen(5000);
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
# Features
|
|
57
|
+
|
|
58
|
+
## Logger Integration (Production-Ready)
|
|
59
|
+
|
|
60
|
+
### Usage Example
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import pino from "pino";
|
|
64
|
+
const logger = pino();
|
|
65
|
+
|
|
66
|
+
app.use(
|
|
67
|
+
errorMiddleware({
|
|
68
|
+
logger: logger.error.bind(logger),
|
|
69
|
+
showStack: true
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Now it supports:
|
|
75
|
+
|
|
76
|
+
- console
|
|
77
|
+
- winston
|
|
78
|
+
- pino
|
|
79
|
+
- custom logger
|
package/dist/index.cjs
CHANGED
|
@@ -46,7 +46,7 @@ var ApiError = class extends Error {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
// src/errorMiddleware.ts
|
|
49
|
-
var errorMiddleware = (err, req, res, next) => {
|
|
49
|
+
var errorMiddleware = (options = {}) => (err, req, res, next) => {
|
|
50
50
|
let statusCode = err.statusCode || 500;
|
|
51
51
|
let message = err.message || "Internal Server Error";
|
|
52
52
|
if (err instanceof ApiError) {
|
|
@@ -57,11 +57,16 @@ var errorMiddleware = (err, req, res, next) => {
|
|
|
57
57
|
if (isProduction && statusCode === 500) {
|
|
58
58
|
message = "Internal Server Error";
|
|
59
59
|
}
|
|
60
|
+
if (options.logger) {
|
|
61
|
+
options.logger(err);
|
|
62
|
+
} else {
|
|
63
|
+
console.error(err);
|
|
64
|
+
}
|
|
60
65
|
res.status(statusCode).json({
|
|
61
66
|
success: false,
|
|
62
67
|
statusCode,
|
|
63
68
|
message,
|
|
64
|
-
...isProduction ? {
|
|
69
|
+
...options.showStack && !isProduction ? { stack: err.stack } : {}
|
|
65
70
|
});
|
|
66
71
|
};
|
|
67
72
|
|
package/dist/index.d.cts
CHANGED
|
@@ -8,7 +8,12 @@ declare class ApiError extends Error {
|
|
|
8
8
|
constructor(statusCode: number, message: string, isOperational?: boolean);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
interface ErrorMiddlewareOptions {
|
|
12
|
+
logger?: (error: any) => void;
|
|
13
|
+
showStack?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const errorMiddleware: (options?: ErrorMiddlewareOptions) => (err: any, req: Request, res: Response, next: NextFunction) => void;
|
|
12
17
|
|
|
13
18
|
declare const notFoundMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
19
|
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,12 @@ declare class ApiError extends Error {
|
|
|
8
8
|
constructor(statusCode: number, message: string, isOperational?: boolean);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
interface ErrorMiddlewareOptions {
|
|
12
|
+
logger?: (error: any) => void;
|
|
13
|
+
showStack?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const errorMiddleware: (options?: ErrorMiddlewareOptions) => (err: any, req: Request, res: Response, next: NextFunction) => void;
|
|
12
17
|
|
|
13
18
|
declare const notFoundMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
19
|
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ var ApiError = class extends Error {
|
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
// src/errorMiddleware.ts
|
|
20
|
-
var errorMiddleware = (err, req, res, next) => {
|
|
20
|
+
var errorMiddleware = (options = {}) => (err, req, res, next) => {
|
|
21
21
|
let statusCode = err.statusCode || 500;
|
|
22
22
|
let message = err.message || "Internal Server Error";
|
|
23
23
|
if (err instanceof ApiError) {
|
|
@@ -28,11 +28,16 @@ var errorMiddleware = (err, req, res, next) => {
|
|
|
28
28
|
if (isProduction && statusCode === 500) {
|
|
29
29
|
message = "Internal Server Error";
|
|
30
30
|
}
|
|
31
|
+
if (options.logger) {
|
|
32
|
+
options.logger(err);
|
|
33
|
+
} else {
|
|
34
|
+
console.error(err);
|
|
35
|
+
}
|
|
31
36
|
res.status(statusCode).json({
|
|
32
37
|
success: false,
|
|
33
38
|
statusCode,
|
|
34
39
|
message,
|
|
35
|
-
...isProduction ? {
|
|
40
|
+
...options.showStack && !isProduction ? { stack: err.stack } : {}
|
|
36
41
|
});
|
|
37
42
|
};
|
|
38
43
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@erangamadhushan/express-advanced-error-kit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Advanced TypeScript error handling middleware for Express applications.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
12
|
-
"prepublishOnly": "npm run build"
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
13
|
+
"test": "jest --coverage"
|
|
13
14
|
},
|
|
14
15
|
"repository": {
|
|
15
16
|
"type": "git",
|
|
@@ -29,7 +30,19 @@
|
|
|
29
30
|
},
|
|
30
31
|
"homepage": "https://github.com/Erangamadhushan/express-advanced-error-kit#readme",
|
|
31
32
|
"devDependencies": {
|
|
33
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
34
|
+
"@semantic-release/git": "^10.0.1",
|
|
35
|
+
"@semantic-release/github": "^12.0.6",
|
|
36
|
+
"@semantic-release/npm": "^13.1.4",
|
|
32
37
|
"@types/express": "^5.0.6",
|
|
38
|
+
"@types/jest": "^30.0.0",
|
|
39
|
+
"@types/supertest": "^6.0.3",
|
|
40
|
+
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
41
|
+
"express": "^5.2.1",
|
|
42
|
+
"jest": "^30.2.0",
|
|
43
|
+
"semantic-release": "^25.0.3",
|
|
44
|
+
"supertest": "^7.2.2",
|
|
45
|
+
"ts-jest": "^29.4.6",
|
|
33
46
|
"tsup": "^8.5.1",
|
|
34
47
|
"typescript": "^5.9.3"
|
|
35
48
|
}
|