natural-language-api-google/middleware/error-handler.js

29 lines
933 B
JavaScript
Raw Normal View History

2023-10-09 17:22:48 +00:00
const { StatusCodes } = require('http-status-codes');
const errorHandlerMiddleware = (err, req, res, next) => {
let customError = {
// set default
statusCode: err.statusCode || StatusCodes.INTERNAL_SERVER_ERROR,
msg: err.message || 'Something went wrong try again later',
};
if (err.name === 'ValidationError') {
customError.msg = Object.values(err.errors)
.map((item) => item.message)
.join(',');
customError.statusCode = 400;
}
if (err.code && err.code === 11000) {
customError.msg = `Duplicate value entered for ${Object.keys(
err.keyValue
)} field, please choose another value`;
customError.statusCode = 400;
}
if (err.name === 'CastError') {
customError.msg = `No item found with id : ${err.value}`;
customError.statusCode = 404;
}
return res.status(customError.statusCode).json({ msg: customError.msg });
};
module.exports = errorHandlerMiddleware;