Skip to content

Commit bdde11c

Browse files
committed
error files added
1 parent b77fe3c commit bdde11c

File tree

8 files changed

+99
-0
lines changed

8 files changed

+99
-0
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
"description": "A lightweight and developer-friendly toolkit for robust error handling in Express.js applications.\nIncludes ready-to-use custom error classes, an async route error handler wrapper, a global error handler middleware, and a convenient 'not found' route handler.\nDesigned to simplify error management, reduce boilerplate, and improve app reliability — all with TypeScript support and easy integration.",
1212
"dependencies": {
1313
"http-status-toolkit": "^1.0.3"
14+
},
15+
"devDependencies": {
16+
"typescript": "^5.8.3"
1417
}
1518
}

src/error/bad-request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { StatusCodes } from "../constants/httpStatus";
2+
import CustomAPIError from "./custom-api";
3+
4+
5+
class BadRequestError extends CustomAPIError {
6+
constructor(message: string, details: string | object | null = null) {
7+
super(message, StatusCodes.BAD_REQUEST, details);
8+
}
9+
}
10+
11+
export default BadRequestError;

src/error/custom-api.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { StatusCodes } from "../constants/httpStatus";
2+
3+
class CustomAPIError extends Error {
4+
5+
public statusCode: number;
6+
public details: string | object | null;
7+
8+
constructor(message: string, statusCode: number = StatusCodes.UNAUTHORIZED, details: string | object | null = null) {
9+
super(message);
10+
this.statusCode = statusCode;
11+
this.details = details;
12+
13+
// Set the prototype explicitly to maintain the correct prototype chain
14+
Object.setPrototypeOf(this, CustomAPIError.prototype);
15+
}
16+
}
17+
18+
19+
export default CustomAPIError;

src/error/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import CustomAPIError from './custom-api';
2+
import BadRequestError from './bad-request';
3+
import NotFoundError from './not-found';
4+
import UnauthenticatedError from './unauthenticated';
5+
6+
export {
7+
CustomAPIError,
8+
BadRequestError,
9+
NotFoundError,
10+
UnauthenticatedError
11+
}

src/error/not-found.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { StatusCodes } from "../constants/httpStatus";
2+
import CustomAPIError from "./custom-api";
3+
4+
5+
class NotFoundError extends CustomAPIError {
6+
constructor(message: string, details: string | object | null = null) {
7+
super(message, StatusCodes.NOT_FOUND, details);
8+
}
9+
}
10+
11+
export default NotFoundError;

src/error/unauthenticated.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { StatusCodes } from "../constants/httpStatus";
2+
import CustomAPIError from "./custom-api";
3+
4+
5+
class UnauthenticatedError extends CustomAPIError {
6+
constructor(message: string, details: string | object | null = null) {
7+
super(message, StatusCodes.UNAUTHORIZED, details);
8+
}
9+
}
10+
11+
export default UnauthenticatedError;

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "ESNext",
5+
"moduleResolution": "node",
6+
"declaration": true,
7+
"declarationDir": "dist",
8+
"outDir": "./dist",
9+
"rootDir": "./src",
10+
"strict": true,
11+
"esModuleInterop": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"skipLibCheck": true
14+
},
15+
"include": ["src"]
16+
}

0 commit comments

Comments
 (0)