23 lines
849 B
JavaScript
23 lines
849 B
JavaScript
const ApplicationError = require('../utils/application-error');
|
|
|
|
/**
|
|
* Middleware to handle errors at the application level.
|
|
*
|
|
* This middleware handles different types of errors.
|
|
* It generates an appropriate response based on the type of error and the error message.
|
|
*
|
|
* @param {Error} err - The error to be handled.
|
|
* @param {Object} req - The Express request object.
|
|
* @param {Object} res - The Express response object.
|
|
* @param {function} next - The function to move to the next middleware.
|
|
*/
|
|
module.exports = async (err, req, res, next) => {
|
|
if (err instanceof ApplicationError) {
|
|
console.error(err);
|
|
return res.status(err.httpStatusCode).json({ error: err.message, code: err.code, isSuccess: false, body: err.body });
|
|
}
|
|
|
|
console.error(err);
|
|
return res.status(500).json({ error: err.message, code: -1, isSuccess: false });
|
|
};
|