This commit is contained in:
Mr Swift
2024-04-27 20:10:55 +03:30
parent f52f5496c5
commit ee6bd01224
30 changed files with 6503 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
const { constants } = require("../constants");
const errorHandler = (err, req, res, next) => {
const statusCode = res.statusCode ? res.statusCode : 500;
switch (statusCode) {
case constants.VALIDATION_ERROR:
res.status(statusCode).json({
title: "Validation Failed ",
msg: err.message,
stackTrace: err.stack,
});
break;
case constants.NOT_FOUND:
res.status(statusCode).json({
title: "Not found ",
msg: err.message,
stackTrace: err.stack,
});
break;
case constants.FORBIDOEN:
res.status(statusCode).json({
title: "Forbidoen ",
msg: err.message,
stackTrace: err.stack,
});
break;
case constants.SERVER_ERROR:
res.status(statusCode).json({
title: "Server Error ",
msg: err.message,
stackTrace: err.stack,
});
break;
case constants.UNAUTHORIZED:
res.status(statusCode).json({
title: "Unauthorized ",
msg: err.message,
stackTrace: err.stack,
});
break;
case constants.SITE_UNAUTHORIZED:
res.status(statusCode).json({
title: "Site Unauthorized ",
msg: err.message,
stackTrace: err.stack,
});
break;
case constants.TOO_MANY:
res.status(statusCode).json({
title: "Too many request",
msg: err.message,
stackTrace: err.stack,
});
break;
default:
console.log(err);
res.status(500).json({ err: err.message, msg: "Unhandled error" })
break;
}
};
module.exports = { name: "errorHandler", run: errorHandler };