@erangamadhushan/express-advanced-error-kit 1.4.0 โ†’ 1.5.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 CHANGED
@@ -3,30 +3,32 @@
3
3
  Advanced TypeScript-based error handling middleware for Express.js.
4
4
 
5
5
  ![npm version](https://img.shields.io/npm/v/@erangamadhushan/express-advanced-error-kit)
6
- ![npm downloads](https://img.shields.io/npm/dm/@erangamadhusha/express-advanced-error-kit)
7
-
8
- ![CI](https://github.com/Erangamadhushan/express-advanced-error-kit/actions/workflows/ci.yml/badge.svg)
6
+ ![npm downloads](https://img.shields.io/npm/dm/@erangamadhushan/express-advanced-error-kit)
9
7
 
8
+ ---
10
9
 
11
- ## Features
10
+ ## โœจ Features
12
11
 
13
12
  - Async handler wrapper
14
- - Custom ApiError class
13
+ - Custom `ApiError` class
15
14
  - Global error middleware
16
- - 404 not found middleware
17
- - Production-safe stack hiding
15
+ - 404 Not Found middleware
16
+ - Logger integration (Pino, Winston, custom)
17
+ - MongoDB duplicate key smart parsing
18
+ - Zod validation error formatting
19
+ - Production-safe stack handling
18
20
  - ESM + CommonJS support
19
- - Full TypeScript definitions
21
+ - Full TypeScript support
20
22
 
21
23
  ---
22
24
 
23
- ## Installation
25
+ ## ๐Ÿ“ฆ Installation
24
26
 
25
27
  ```bash
26
28
  npm install @erangamadhushan/express-advanced-error-kit
27
29
  ```
28
30
 
29
- ## Usage
31
+ # ๐Ÿš€ Quick Start
30
32
 
31
33
  ```ts
32
34
  import express from "express";
@@ -34,46 +36,116 @@ import {
34
36
  asyncHandler,
35
37
  ApiError,
36
38
  notFoundMiddleware,
37
- errorMiddleware
39
+ errorMiddleware,
38
40
  } from "@erangamadhushan/express-advanced-error-kit";
39
41
 
40
42
  const app = express();
43
+ app.use(express.json());
41
44
 
42
45
  app.get(
43
46
  "/error",
44
47
  asyncHandler(async () => {
45
48
  throw new ApiError("Something went wrong", 400);
46
- })
49
+ }),
47
50
  );
48
51
 
49
52
  app.use(notFoundMiddleware);
50
- app.use(errorMiddleware);
53
+ app.use(errorMiddleware());
51
54
 
52
55
  app.listen(5000);
56
+ ```
57
+
58
+ # ๐Ÿง  Smart MongoDB Error Handling
59
+
60
+ ## Duplicate key errors are automatically formatted:
61
+
62
+ ```ts
63
+ // Mongo duplicate key error
64
+ {
65
+ code: 11000,
66
+ keyValue: { email: "test@example.com" }
67
+ }
68
+ ```
53
69
 
70
+ Response:
71
+
72
+ ```json
73
+ {
74
+ "success": false,
75
+ "message": "email already exists"
76
+ }
77
+ ```
78
+
79
+ # ๐Ÿงพ Zod Validation Formatting
80
+
81
+ If using Zod:
82
+
83
+ ```ts
84
+ throw new ZodError([...]);
54
85
  ```
55
86
 
56
- # Features
87
+ Response:
88
+
89
+ ```json
90
+ {
91
+ "success": false,
92
+ "message": "email: Invalid email"
93
+ }
94
+ ```
57
95
 
58
- ## Logger Integration (Production-Ready)
96
+ # ๐Ÿชต Logger Integration
59
97
 
60
- ### Usage Example
98
+ Use any logger:
61
99
 
62
100
  ```ts
63
101
  import pino from "pino";
102
+
64
103
  const logger = pino();
65
104
 
66
105
  app.use(
67
106
  errorMiddleware({
68
107
  logger: logger.error.bind(logger),
69
- showStack: true
70
- })
108
+ showStack: false,
109
+ }),
71
110
  );
72
111
  ```
73
112
 
74
- ### Now it supports:
113
+ # ๐Ÿ“š Middleware Order (Important)
114
+
115
+ ```js
116
+ app.use(routes);
117
+ ........
118
+ ........
119
+ app.use(notFoundMiddleware);
120
+ app.use(errorMiddleware());
121
+ ```
122
+
123
+ # ๐Ÿงฉ Creating Custom Errors
124
+
125
+ ```ts
126
+ throw new ApiError(404, "User not found");
127
+ ```
128
+
129
+ Also You can extend it:
130
+ ```ts
131
+ class ValidationError extends ApiError {
132
+ constructor(message: string) {
133
+ super(400, message);
134
+ }
135
+ }
136
+ ```
137
+
138
+ # โš™๏ธ Configuration Options
139
+ ```ts
140
+ errorMiddleware({
141
+ logger?: (error: any) => void;
142
+ showStack?: boolean;
143
+ });
144
+ ```
145
+
146
+ # ๐Ÿ›ก Production Behavior
147
+
148
+ - Stack traces hidden automatically in production
149
+ - Clean JSON response format
150
+ - Centralized error control
75
151
 
76
- - console
77
- - winston
78
- - pino
79
- - custom logger
package/dist/index.cjs CHANGED
@@ -86,8 +86,6 @@ var errorMiddleware = (options = {}) => (err, req, res, next) => {
86
86
  statusCode = zodError.statusCode;
87
87
  message = zodError.message;
88
88
  }
89
- statusCode = mongoError instanceof ApiError ? mongoError.statusCode : zodError instanceof ApiError ? zodError.statusCode : 500;
90
- message = mongoError instanceof ApiError ? mongoError.message : zodError instanceof ApiError ? zodError.message : "Internal Server Error";
91
89
  res.status(statusCode).json({
92
90
  success: false,
93
91
  statusCode,
package/dist/index.js CHANGED
@@ -57,8 +57,6 @@ var errorMiddleware = (options = {}) => (err, req, res, next) => {
57
57
  statusCode = zodError.statusCode;
58
58
  message = zodError.message;
59
59
  }
60
- statusCode = mongoError instanceof ApiError ? mongoError.statusCode : zodError instanceof ApiError ? zodError.statusCode : 500;
61
- message = mongoError instanceof ApiError ? mongoError.message : zodError instanceof ApiError ? zodError.message : "Internal Server Error";
62
60
  res.status(statusCode).json({
63
61
  success: false,
64
62
  statusCode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erangamadhushan/express-advanced-error-kit",
3
- "version": "1.4.0",
3
+ "version": "1.5.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",
@@ -51,6 +51,7 @@
51
51
  "zod": "^4.3.6"
52
52
  },
53
53
  "dependencies": {
54
+ "dotenv": "^17.3.1",
54
55
  "zod": "^4.3.6"
55
56
  }
56
57
  }