• Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

125
Views
TypeError: _middleware_error__WEBPACK_IMPORTED_MODULE_2__.default is not a constructor

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.

9 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.