Colubrina/backend/lib/errorHandler.js

27 lines
632 B
JavaScript
Raw Normal View History

2022-06-05 22:58:18 +10:00
let { Sequelize } = require("../models")
let Errors = require("./errors")
2022-08-14 22:06:56 +10:00
const multer = require("multer")
2022-06-05 22:58:18 +10:00
module.exports = function (err, req, res, next) {
if (err instanceof Sequelize.ValidationError) {
res.status(400).json(err)
} else if (err.name in Errors) {
res.status(err.status).json({
errors: [err]
})
} else {
console.error(err)
2022-08-14 22:06:56 +10:00
if (err instanceof multer.MulterError) {
if (err.code === "LIMIT_FILE_SIZE") {
return res.status(400).json({
errors: [Errors.fileTooLarge]
})
}
}
2022-06-05 22:58:18 +10:00
res.status(500).json({
errors: [Errors.unknown]
})
}
}