Spaces:
Running
Running
Commit
·
67bacb2
1
Parent(s):
61c7d9c
feat: improve error handler with error messages
Browse files- src/middlewares/error-handler.middleware.ts +59 -0
- src/routes.ts +2 -15
src/middlewares/error-handler.middleware.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JsonResponse } from "@lib/responses/json-response";
|
| 2 |
+
import { NextFunction, Request, Response } from "express";
|
| 3 |
+
import { Error as MongooseError } from "mongoose";
|
| 4 |
+
|
| 5 |
+
export const errorHandlerMiddleware = (
|
| 6 |
+
err: MongooseError | any,
|
| 7 |
+
req: Request,
|
| 8 |
+
res: Response,
|
| 9 |
+
next: NextFunction
|
| 10 |
+
) => {
|
| 11 |
+
let errorMessage = err.message;
|
| 12 |
+
let status = err.status || 500;
|
| 13 |
+
console.log("errorHandlerMiddleware -> err", err);
|
| 14 |
+
|
| 15 |
+
if (err.code === 11000) {
|
| 16 |
+
// Handle validation errors
|
| 17 |
+
const errors = [];
|
| 18 |
+
try {
|
| 19 |
+
const errorInKeys = Object.keys(err.keyValue);
|
| 20 |
+
errorInKeys.forEach((key) => {
|
| 21 |
+
errors.push(`${key} already exists`);
|
| 22 |
+
});
|
| 23 |
+
} catch (error) {
|
| 24 |
+
errors.push("Duplicate key error");
|
| 25 |
+
}
|
| 26 |
+
return JsonResponse.validationError(
|
| 27 |
+
{
|
| 28 |
+
errors,
|
| 29 |
+
},
|
| 30 |
+
res
|
| 31 |
+
);
|
| 32 |
+
} else if (err.code === 66) {
|
| 33 |
+
// Handle cast errors
|
| 34 |
+
status = 400;
|
| 35 |
+
if (err.path) {
|
| 36 |
+
errorMessage = `Invalid ${err.path}`;
|
| 37 |
+
} else {
|
| 38 |
+
errorMessage = "Invalid data";
|
| 39 |
+
}
|
| 40 |
+
} else if (err.code === 2) {
|
| 41 |
+
// Handle other mongoose errors
|
| 42 |
+
status = 400;
|
| 43 |
+
if (err.message) {
|
| 44 |
+
errorMessage = err.message;
|
| 45 |
+
} else {
|
| 46 |
+
errorMessage = "Invalid data";
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
JsonResponse.error(
|
| 51 |
+
{
|
| 52 |
+
error: errorMessage,
|
| 53 |
+
status,
|
| 54 |
+
},
|
| 55 |
+
res
|
| 56 |
+
);
|
| 57 |
+
|
| 58 |
+
console.error(err.message, err.stack);
|
| 59 |
+
};
|
src/routes.ts
CHANGED
|
@@ -5,6 +5,7 @@ import path from "path";
|
|
| 5 |
import { BaseController } from "./lib/controllers/controller.base";
|
| 6 |
import { validationErrorHandler } from "./helpers/validation.helper";
|
| 7 |
import { JsonResponse } from "@lib/responses/json-response";
|
|
|
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Sets the routes for the Express app.
|
|
@@ -54,21 +55,7 @@ const setCustomRoutes = (router: Router) => {
|
|
| 54 |
});
|
| 55 |
|
| 56 |
// Error handler
|
| 57 |
-
router.use(
|
| 58 |
-
try {
|
| 59 |
-
err.message = JSON.parse(err.message);
|
| 60 |
-
} catch (error) {}
|
| 61 |
-
|
| 62 |
-
JsonResponse.error(
|
| 63 |
-
{
|
| 64 |
-
error: err.message || "Internal Server Error",
|
| 65 |
-
status: err.status || 500,
|
| 66 |
-
},
|
| 67 |
-
res
|
| 68 |
-
);
|
| 69 |
-
|
| 70 |
-
console.error(err.message, err.stack);
|
| 71 |
-
});
|
| 72 |
};
|
| 73 |
|
| 74 |
/* importing all controllers */
|
|
|
|
| 5 |
import { BaseController } from "./lib/controllers/controller.base";
|
| 6 |
import { validationErrorHandler } from "./helpers/validation.helper";
|
| 7 |
import { JsonResponse } from "@lib/responses/json-response";
|
| 8 |
+
import { errorHandlerMiddleware } from "middlewares/error-handler.middleware";
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Sets the routes for the Express app.
|
|
|
|
| 55 |
});
|
| 56 |
|
| 57 |
// Error handler
|
| 58 |
+
router.use(errorHandlerMiddleware);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
};
|
| 60 |
|
| 61 |
/* importing all controllers */
|