Skip to content

Commit 9aa2ecd

Browse files
committed
not found handler added and converted default exports to named exports
1 parent 797992b commit 9aa2ecd

File tree

8 files changed

+40
-28
lines changed

8 files changed

+40
-28
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Request, Response, NextFunction } from 'express';
22

33

4-
const asyncHandler = (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => {
4+
export const asyncHandler = (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => {
55
return (req: Request, res: Response, next: NextFunction) => {
66
Promise.resolve(fn(req, res, next)).catch(next)
77
}
88
}
99

10-
export default asyncHandler;
1110

1211

1312

src/error/bad-request.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { StatusCodes } from "http-status-toolkit";
2-
import CustomAPIError from "./custom-api";
2+
import {CustomAPIError} from "./custom-api";
33

44

5-
class BadRequestError extends CustomAPIError {
5+
export class BadRequestError extends CustomAPIError {
66
constructor(message: string, details: string | object | null = null) {
77
super(message, StatusCodes.BAD_REQUEST, details);
88
}
99
}
10-
11-
export default BadRequestError;

src/error/custom-api.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StatusCodes } from "http-status-toolkit";
22

3-
class CustomAPIError extends Error {
3+
export class CustomAPIError extends Error {
44

55
public statusCode: number;
66
public details: string | object | null;
@@ -15,5 +15,3 @@ class CustomAPIError extends Error {
1515
}
1616
}
1717

18-
19-
export default CustomAPIError;

src/error/index.ts

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

66
export {
77
CustomAPIError,

src/error/not-found.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { StatusCodes } from "http-status-toolkit";
2-
import CustomAPIError from "./custom-api";
2+
import {CustomAPIError} from "./custom-api";
33

44

5-
class NotFoundError extends CustomAPIError {
5+
export class NotFoundError extends CustomAPIError {
66
constructor(message: string, details: string | object | null = null) {
77
super(message, StatusCodes.NOT_FOUND, details);
88
}
99
}
10-
11-
export default NotFoundError;

src/error/unauthenticated.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { StatusCodes } from "http-status-toolkit";
2-
import CustomAPIError from "./custom-api";
2+
import {CustomAPIError} from "./custom-api";
33

44

5-
class UnauthenticatedError extends CustomAPIError {
5+
export class UnauthenticatedError extends CustomAPIError {
66
constructor(message: string, details: string | object | null = null) {
77
super(message, StatusCodes.UNAUTHORIZED, details);
88
}
99
}
10-
11-
export default UnauthenticatedError;

src/globalErrorHandler.ts renamed to src/global-error-handler.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ interface CustomError extends Error {
77
}
88

99

10-
const errorHandlerMiddleware = (err: CustomError, req: Request, res: Response, next: NextFunction) => {
10+
export const globalErrorHandler = (
11+
err: CustomError,
12+
req: Request,
13+
res: Response,
14+
next: NextFunction
15+
) => {
1116
let status = err.statusCode || StatusCodes.INTERNAL_SERVER_ERROR;
12-
let message = err.message || "Something went wrong, please try again later.";
17+
let message = err.message || 'Something went wrong, please try again later.';
1318

14-
const notFound = StatusCodes.NOT_FOUND
19+
const notFound = StatusCodes.NOT_FOUND;
1520

16-
const errorResponse: { success: boolean; error: string; details?: string | null} = {
21+
const errorResponse: {
22+
success: boolean;
23+
error: string;
24+
details?: string | null;
25+
} = {
1726
success: false,
1827
error: message,
1928
};
@@ -23,6 +32,4 @@ const errorHandlerMiddleware = (err: CustomError, req: Request, res: Response, n
2332
}
2433

2534
res.status(status).json(errorResponse);
26-
}
27-
28-
export default errorHandlerMiddleware;
35+
};

src/not-found.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Request, Response } from 'express';
2+
import { StatusCodes } from 'http-status-toolkit';
3+
4+
5+
6+
export const notFoundHandler = (req: Request, res: Response, ) => {
7+
res
8+
.status(StatusCodes.NOT_FOUND)
9+
.json({
10+
success: false,
11+
error: "Not Found",
12+
message: `Route not found: ${req.originalUrl}`,
13+
});
14+
}

0 commit comments

Comments
 (0)