-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy patherror.js
35 lines (29 loc) · 1.07 KB
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const ErrorHandler = require("../utils/errorhandler");
module.exports = (err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.message = err.message || "Internal Server Error";
//Wrong Mongodb ID Error
if (err.name === "CastError") {
const message = `Resource not found. Invalid ${err.path}`; //err.path -> line at which error has occured
err = new ErrorHandler(message, 400);
}
//Mongoose Duplicate key error
if (err.code === 11000) {
const message = `Duplicate ${Object.keys(err.keyValue)} Entered`; // 'Object.keys(err.keyValue)' is use to retrieve whatever the error is.
err = new ErrorHandler(message, 400);
}
//Wrong JWT error
if (err.name === "JsonWebTokenError") {
const message = `Json Web Token is invalid, Try again`;
err = new ErrorHandler(message, 400);
}
//JWT Expire Error
if (err.name === "TokenExpiredError") {
const message = `Json Web Token is Expired, Try again`;
err = new ErrorHandler(message, 400);
}
res.status(err.statusCode).json({
success: false,
message: err.message,
});
};