Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x | const logger = require("./logger");
class ApiError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode || 500;
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
// eslint-disable-next-line no-unused-vars
const errorHandler = (err, _req, res, _next) => {
const statusCode = err.statusCode || 500;
const message = err.message || "Internal Server Error";
// Log the error stack
logger.error(`Error occurred: ${err.message}`, { stack: err.stack });
// For any unexpected errors
return res.status(statusCode).json({
success: false,
message,
error: err.stack ? err.stack : undefined,
});
};
const handleErrors = (res, messsage, statusCode) => {
res.status(statusCode).json({
status: false,
message: messsage,
data: {},
});
return;
};
module.exports = { ApiError, errorHandler, handleErrors };
|