Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
TypeError: _utils_errorhandler__WEBPACK_IMPORTED_MODULE_1___default(...) is not a constructor

I am making a ErrorHandler.js to handle error in my nextJs api, I previously used this in my node-express server. But when the API returns an error instead of showing me the error it returns me this - TypeError: _utils_errorhandler__WEBPACK_IMPORTED_MODULE_1___default(...) is not a constructor I have made a class known as Errorhandler here is the code for it :

../utils/errorHandler.js file:

class ErrorHandler extends Error {
  constructor(message, statusCode) {
    super(message);
    this.statusCode = statusCode;

    Error.captureStackTrace(this, this.constructor);
  }
}

module.exports = ErrorHandler;

Then I made a middleware function using this class and marked all my error init, here is the code:

../../middleware/error.js file:

const ErrorHandler = require("../utils/errorhandler");

module.exports = (err, req, res, next) => {
  err.statusCode = err.statusCode || 500;
  err.message = err.message || "Internal Server Error";

  // Wrong Mongodb Id error
  if (err.name === "CastError") {
    const message = `Resource not found. Invalid: ${err.path}`;
    err = new ErrorHandler(message, 400);
  }

  // Mongoose duplicate key error
  if (err.code === 11000) {
    const message = `Duplicate ${Object.keys(err.keyValue)} Entered`;
    err = new ErrorHandler(message, 400);
  }

  // Wrong JWT error
  if (err.name === "JsonWebTokenError") {
    const message = `Json Web Token is invalid, Try again `;
    err = new ErrorHandler(message, 400);
  }

  // JWT EXPIRE error
  if (err.name === "TokenExpiredError") {
    const message = `Json Web Token is Expired, Try again `;
    err = new ErrorHandler(message, 400);
  }

  res.status(err.statusCode).json({
    success: false,
    message: err.message,
  });
};

I tried changing it to ES6 module but it didn't worked for me. The API code is here :

import connectToMongo from "../../middleware/db";
const ErrorHandler = require("../../middleware/error");

const handler = (req, res, next) => {
  return next(new ErrorHandler("Test successful", 404));
};

export default connectToMongo(handler);

I am testing the ErrorHandler here so and it returns me the same error in all my API routes It also returns next() is not a function some times.

The full Error is here :

error - (api)\pages\api\test.js (5:14) @ handler
TypeError: _utils_errorhandler__WEBPACK_IMPORTED_MODULE_1___default(...) is not a constructor
  3 |
  4 | const handler = (req, res, next) => {
> 5 |   return next(new ErrorHandler("Test successful", 404));
    |              ^
  6 | };
  7 |
  8 | export default connectToMongo(handler);
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda