I am making a Login API in NEXT JS, I have made an error handler, but when I call login API it gives me the following error :
TypeError: _middleware_error__WEBPACK_IMPORTED_MODULE_2__.default is not a constructor
} catch (error) {
return next(
new ErrorHandler({ "Internal Server Error : ": error.message }, 500)
^
);
}});
Here is my ErrorHandler File :
const ErrorHandler = require("../utils/errorhandler");
module.exports = (err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.message = err.message || "Internal Server Error";
//Mongo DB Error
if (err.name === "CastError") {
const message = `Resource Not Found. Invalid: ${err.path}`;
err = new ErrorHandler(message, 400);
}
res.status(err.statusCode).json({
success: false,
message: err.message,
statusCode: err.statusCode,
});
};
I am importing a class constructor from my utils/errorhandler here is the code:
class ErrorHander extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = ErrorHander;
So basically I am creating a class known as ErrorHandler and then using it to make a error handling function which returns me the error message and the status code. The error says I have some issue with the function, I tried changing it to ES6 but it didn't worked.
I am using NEXT JS API with custom Express Server for backend.
I appreciate your efforts for helping me with this Thank You.