express-error-kit-siri 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +74 -2
- package/package.json +39 -4
- package/src/AppError.js +10 -0
- package/src/asyncHandler.js +11 -0
- package/src/errorHandler.js +16 -0
- package/src/index.js +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Express Error Kit contributors
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Express Error Kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A tiny CommonJS package for consistent JSON error responses in Express 4 and 5 applications.
|
|
4
|
+
|
|
5
|
+
## Why use it?
|
|
6
|
+
|
|
7
|
+
Avoid repeating async error forwarding and response formatting in each route. Server error messages are hidden when `NODE_ENV=production`.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install express-error-kit-siri
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Basic usage
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const express = require('express');
|
|
19
|
+
const { AppError, asyncHandler, errorHandler } = require('express-error-kit');
|
|
20
|
+
const app = express();
|
|
21
|
+
|
|
22
|
+
app.get('/users/:id', asyncHandler(async (req, res) => {
|
|
23
|
+
if (req.params.id !== '1') {
|
|
24
|
+
throw new AppError('User not found', 404, 'USER_NOT_FOUND');
|
|
25
|
+
}
|
|
26
|
+
res.json({ id: 1 });
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
// Register after all routes.
|
|
30
|
+
app.use(errorHandler);
|
|
31
|
+
app.listen(3000);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
For a missing user, the response has HTTP status 404:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{"success":false,"error":{"message":"User not found","code":"USER_NOT_FOUND","statusCode":404}}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
- `new AppError(message, statusCode = 500, code = 'APP_ERROR')`: returns an `Error` with `message` (string), `statusCode` (HTTP status number), and `code` (application error identifier).
|
|
43
|
+
- `asyncHandler(fn)`: accepts an Express route or middleware function `(req, res, next)` and returns a middleware function that forwards thrown errors and rejected promises to `next`. In Express 5, async rejections are already forwarded automatically, but this helper also works with Express 4.
|
|
44
|
+
- `errorHandler(err, req, res, next)`: Express error middleware; returns JSON `{ success: false, error: { message, code, statusCode } }`. Use **after** routes. Calls `next(err)` if headers have already been sent.
|
|
45
|
+
|
|
46
|
+
## Testing
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm test
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Example server
|
|
53
|
+
|
|
54
|
+
From this repository:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm install express
|
|
58
|
+
node examples/server.js
|
|
59
|
+
# visit http://localhost:3000/users/2
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Limitations
|
|
63
|
+
|
|
64
|
+
- CommonJS only; ESM projects can use a default import from CommonJS.
|
|
65
|
+
- This package does not log errors, validate request bodies, or handle non-Express processes.
|
|
66
|
+
- In production, messages for HTTP 5xx responses are replaced with `Internal Server Error`; 4xx messages are shown. Avoid putting secrets in client-facing 4xx messages.
|
|
67
|
+
- `AppError` does not validate constructor arguments; supply valid HTTP status codes.
|
|
68
|
+
|
|
69
|
+
## Contributing
|
|
70
|
+
|
|
71
|
+
Open an issue describing the change, create a branch, add tests, run `npm test`, and open a pull request.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-error-kit-siri",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Small, configurable error handling utilities for Express apps",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"README.md",
|
|
9
|
+
"LICENSE"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "node --test",
|
|
13
|
+
"pack:check": "npm pack --dry-run"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"express",
|
|
17
|
+
"error",
|
|
18
|
+
"middleware",
|
|
19
|
+
"async-handler"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/maddikasirireddy/express-error-kit.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/maddikasirireddy/express-error-kit#readme",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/maddikasirireddy/express-error-kit/issues"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"express": "^4.18.0 || ^5.0.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"express": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/AppError.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class AppError extends Error {
|
|
2
|
+
constructor(message, statusCode = 500, code = 'APP_ERROR') {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'AppError';
|
|
5
|
+
this.statusCode = statusCode;
|
|
6
|
+
this.code = code;
|
|
7
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
module.exports = AppError;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function asyncHandler(fn) {
|
|
2
|
+
if (typeof fn !== 'function') throw new TypeError('asyncHandler expects a function');
|
|
3
|
+
return function wrapped(req, res, next) {
|
|
4
|
+
try {
|
|
5
|
+
Promise.resolve(fn(req, res, next)).catch(next);
|
|
6
|
+
} catch (error) {
|
|
7
|
+
next(error);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
module.exports = asyncHandler;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function errorHandler(err, req, res, next) {
|
|
2
|
+
if (res.headersSent) return next(err);
|
|
3
|
+
const status = Number.isInteger(err.statusCode) && err.statusCode >= 400 && err.statusCode <= 599
|
|
4
|
+
? err.statusCode : 500;
|
|
5
|
+
const isServerError = status >= 500;
|
|
6
|
+
const production = process.env.NODE_ENV === 'production';
|
|
7
|
+
res.status(status).json({
|
|
8
|
+
success: false,
|
|
9
|
+
error: {
|
|
10
|
+
message: production && isServerError ? 'Internal Server Error' : (err.message || 'Unexpected error'),
|
|
11
|
+
code: err.code || (isServerError ? 'INTERNAL_ERROR' : 'REQUEST_ERROR'),
|
|
12
|
+
statusCode: status
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
module.exports = errorHandler;
|