All files / postcardotp-backend/src/utils errorHandler.js

100% Statements 14/14
62.5% Branches 5/8
100% Functions 3/3
100% Lines 14/14

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 381x       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 };