getryt-demo / src /utils /errors.js
KaboKableMolefe's picture
Sync from GitHub via hub-sync
4ed88f9 verified
Raw
History Blame Contribute Delete
815 Bytes
class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
function errorHandler(err, _req, res, _next) {
const statusCode = err.isOperational ? err.statusCode : 500;
const message = err.isOperational ? err.message : 'Internal server error';
console.error(`[ERROR] ${statusCode} - ${err.message}`);
if (!err.isOperational) console.error(err.stack);
res.status(statusCode).json({
error: message,
...(process.env.NODE_ENV === 'development' && { stack: err.stack }),
});
}
function asyncHandler(fn) {
return (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
}
module.exports = { AppError, errorHandler, asyncHandler };