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

99
Visualizações
Javascript - Create custom errors and make sure that constructor params are valid

I have defined the following object

const AuthErrorCode = {
   EMAIL_ALREADY_EXISTS: {
      code: "auth/email-already-exists",
      message: "Hello world!"
   },
   ... more error codes
};

And I am implementing a class that extends Error

class AuthError extends Error {
  constructor(code, message = undefined) {
    switch(code) {
      case "EMAIL_ALREADY_EXISTS":
        code = AuthErrorCode[code].code;
        message = message ?? AuthErrorCode[code].message;
        break;

      default:
        throw new Error("Invalid code");
    }

    super(message); 

    Object.assign(this, {
      code,
      name: "AuthError",
    });
  }
}

which is supposed to receive a code and an optional custom message.

This class has to check that the given code is in the AuthErrorCode object (EMAIL_ALREADY_EXISTS || "auth/email-already-exists" are valid). If it is not inside it, then some kind of feedback should be displayed to the programmer (an error or something). I mean, I need to make sure that the code is a valid AuthErrorCode, because if not, the class is being used incorrectly.

How can I do that? Is it possible?

For example, this code must fail:

throw new AuthError("auth/some-invented-code", "Hello world!");

Example of correct use:

throw new AuthError("EMAIL_ALREADY_EXISTS", "Hello world!");
throw new AuthError("auth/email-already-exists");
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There's a way but ultimately I think it's bad design.

If you already know the set of valid values why not simply expose factory functions for every error types?

e.g.

// Assume encapsulated in a module

class AuthError extends Error {
  constructor(message, code) {
    super(message);
    this.code = code;
  }
}

// This would get exported (factory methods for errors)
const authErrors = Object.freeze({
  emailAlreadyExists(message = 'Hello world!') {
    return new AuthError('auth/email-already-exists', message);
  }

   // other error types
});

// Client usage
throw authErrors.emailAlreadyExists('email already registered');

Alternatively you could create an explicit exception class per error type whic is perhaps more aligned with the Open-Closed Principle:

// Assume encapsulated in a module

class AuthError extends Error {
  constructor(message, code) {
    super(message);
    this.code = code;
  }
}

// Export this
class EmailAdreadyExists extends AuthError {
  constructor(message = "Hello world!") {
    super(message, "auth/email-already-exists");
  }
}

// Client usage
throw new EmailAdreadyExists("email already registered");

about 4 years ago · Juan Pablo Isaza Relatório
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