@erangamadhushan/express-advanced-error-kit 1.0.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 +48 -0
- package/dist/index.cjs +78 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +48 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# express-advanced-error-kit
|
|
2
|
+
|
|
3
|
+
Advanced TypeScript-based error handling middleware for Express.js.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Async handler wrapper
|
|
8
|
+
- Custom ApiError class
|
|
9
|
+
- Global error middleware
|
|
10
|
+
- 404 not found middleware
|
|
11
|
+
- Production-safe stack hiding
|
|
12
|
+
- ESM + CommonJS support
|
|
13
|
+
- Full TypeScript definitions
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install express-advanced-error-kit
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import express from "express";
|
|
27
|
+
import {
|
|
28
|
+
asyncHandler,
|
|
29
|
+
ApiError,
|
|
30
|
+
notFoundMiddleware,
|
|
31
|
+
errorMiddleware
|
|
32
|
+
} from "express-advanced-error-kit";
|
|
33
|
+
|
|
34
|
+
const app = express();
|
|
35
|
+
|
|
36
|
+
app.get(
|
|
37
|
+
"/error",
|
|
38
|
+
asyncHandler(async () => {
|
|
39
|
+
throw new ApiError("Something went wrong", 400);
|
|
40
|
+
})
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
app.use(notFoundMiddleware);
|
|
44
|
+
app.use(errorMiddleware);
|
|
45
|
+
|
|
46
|
+
app.listen(5000);
|
|
47
|
+
|
|
48
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ApiError: () => ApiError,
|
|
24
|
+
asyncHandler: () => asyncHandler,
|
|
25
|
+
errorMiddleware: () => errorMiddleware,
|
|
26
|
+
notFoundMiddleware: () => notFoundMiddleware
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/asyncHandler.ts
|
|
31
|
+
var asyncHandler = (fn) => {
|
|
32
|
+
return (req, res, next) => {
|
|
33
|
+
Promise.resolve(fn(req, res, next)).catch(next);
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/ApiError.ts
|
|
38
|
+
var ApiError = class extends Error {
|
|
39
|
+
constructor(statusCode, message, isOperational = true) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.statusCode = statusCode;
|
|
42
|
+
this.isOperational = isOperational;
|
|
43
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
44
|
+
Error.captureStackTrace(this, this.constructor);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/errorMiddleware.ts
|
|
49
|
+
var errorMiddleware = (err, req, res, next) => {
|
|
50
|
+
let statusCode = err.statusCode || 500;
|
|
51
|
+
let message = err.message || "Internal Server Error";
|
|
52
|
+
if (err instanceof ApiError) {
|
|
53
|
+
statusCode = err.statusCode || 500;
|
|
54
|
+
message = err.message || "Internal Server Error";
|
|
55
|
+
}
|
|
56
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
57
|
+
if (isProduction && statusCode === 500) {
|
|
58
|
+
message = "Internal Server Error";
|
|
59
|
+
}
|
|
60
|
+
res.status(statusCode).json({
|
|
61
|
+
success: false,
|
|
62
|
+
statusCode,
|
|
63
|
+
message,
|
|
64
|
+
...isProduction ? {} : { stack: err.stack }
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/notFoundMiddleware.ts
|
|
69
|
+
var notFoundMiddleware = (req, res, next) => {
|
|
70
|
+
next(new ApiError(404, `Route ${req.originalUrl} not found`));
|
|
71
|
+
};
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
ApiError,
|
|
75
|
+
asyncHandler,
|
|
76
|
+
errorMiddleware,
|
|
77
|
+
notFoundMiddleware
|
|
78
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const asyncHandler: (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => (req: Request, res: Response, next: NextFunction) => void;
|
|
4
|
+
|
|
5
|
+
declare class ApiError extends Error {
|
|
6
|
+
statusCode: number;
|
|
7
|
+
isOperational: boolean;
|
|
8
|
+
constructor(statusCode: number, message: string, isOperational?: boolean);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare const errorMiddleware: (err: any, req: Request, res: Response, next: NextFunction) => void;
|
|
12
|
+
|
|
13
|
+
declare const notFoundMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
|
+
|
|
15
|
+
export { ApiError, asyncHandler, errorMiddleware, notFoundMiddleware };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const asyncHandler: (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => (req: Request, res: Response, next: NextFunction) => void;
|
|
4
|
+
|
|
5
|
+
declare class ApiError extends Error {
|
|
6
|
+
statusCode: number;
|
|
7
|
+
isOperational: boolean;
|
|
8
|
+
constructor(statusCode: number, message: string, isOperational?: boolean);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare const errorMiddleware: (err: any, req: Request, res: Response, next: NextFunction) => void;
|
|
12
|
+
|
|
13
|
+
declare const notFoundMiddleware: (req: Request, res: Response, next: NextFunction) => void;
|
|
14
|
+
|
|
15
|
+
export { ApiError, asyncHandler, errorMiddleware, notFoundMiddleware };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/asyncHandler.ts
|
|
2
|
+
var asyncHandler = (fn) => {
|
|
3
|
+
return (req, res, next) => {
|
|
4
|
+
Promise.resolve(fn(req, res, next)).catch(next);
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/ApiError.ts
|
|
9
|
+
var ApiError = class extends Error {
|
|
10
|
+
constructor(statusCode, message, isOperational = true) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.statusCode = statusCode;
|
|
13
|
+
this.isOperational = isOperational;
|
|
14
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
15
|
+
Error.captureStackTrace(this, this.constructor);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/errorMiddleware.ts
|
|
20
|
+
var errorMiddleware = (err, req, res, next) => {
|
|
21
|
+
let statusCode = err.statusCode || 500;
|
|
22
|
+
let message = err.message || "Internal Server Error";
|
|
23
|
+
if (err instanceof ApiError) {
|
|
24
|
+
statusCode = err.statusCode || 500;
|
|
25
|
+
message = err.message || "Internal Server Error";
|
|
26
|
+
}
|
|
27
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
28
|
+
if (isProduction && statusCode === 500) {
|
|
29
|
+
message = "Internal Server Error";
|
|
30
|
+
}
|
|
31
|
+
res.status(statusCode).json({
|
|
32
|
+
success: false,
|
|
33
|
+
statusCode,
|
|
34
|
+
message,
|
|
35
|
+
...isProduction ? {} : { stack: err.stack }
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/notFoundMiddleware.ts
|
|
40
|
+
var notFoundMiddleware = (req, res, next) => {
|
|
41
|
+
next(new ApiError(404, `Route ${req.originalUrl} not found`));
|
|
42
|
+
};
|
|
43
|
+
export {
|
|
44
|
+
ApiError,
|
|
45
|
+
asyncHandler,
|
|
46
|
+
errorMiddleware,
|
|
47
|
+
notFoundMiddleware
|
|
48
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@erangamadhushan/express-advanced-error-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Advanced TypeScript error handling middleware for Express applications.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
12
|
+
"prepublishOnly": "npm run build"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/Erangamadhushan/express-advanced-error-kit.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"express",
|
|
20
|
+
"middleware",
|
|
21
|
+
"error-handler",
|
|
22
|
+
"typescript"
|
|
23
|
+
],
|
|
24
|
+
"author": "Eranga Madhushan",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/Erangamadhushan/express-advanced-error-kit/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/Erangamadhushan/express-advanced-error-kit#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/express": "^5.0.6",
|
|
33
|
+
"tsup": "^8.5.1",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
}
|
|
36
|
+
}
|