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

151
Visualizações
Custom throw new Error message in JavaScript

I have created my own error by extending Error class:

class MyError extends Error
{
    constructor(message)
    {
        super(message);
        this.name = this.constructor.name;
    }
}

So the following code:

throw new MyError('My Message');

will produce the following output:

throw new MyError('My Message')
^

MyError: My Message <-- OUR MESSAGE
    at Object.<anonymous> (D:\GitHub\error-meta\main.js:1:7)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

We can see that My message is printed between code line and stack trace.

How should I extend Error class to print My message or other text before the standart error output? So it would look like this:

My custom error context data!

/* ... standart error output goes here ... */

If this is not possible, how to completely replace standart error message? Where it even comes from?

Important note: I want this to be displayed in unhanlded throw, without printing data in catch block!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

How should I extend Error class to print "My message" or other text before the standard error output?

You might be able to do this by overwriting the stack property:

class MyError extends Error {
    name = this.constructor.name;
    stack = 'My custom error context data!\n' + this.stack;
}

However, I would not recommend to do this: .stack isn't really standard (yet), may or may not be used by what is actually displaying the error, and you shouldn't mess with the builtin lazy magic getter.

If this is not possible, how to completely replace standard error message? Where it even comes from?

It comes from the nodejs error propagation, which just logs uncaught exceptions before terminating the process. You can override this default behaviour by adding an uncaughtException event handler on process.

However, in the case of a CLI application, you simple shouldn't leave the exception unhandled. Catch it with try/catch around your main function, then do whatever custom error reporting you'd like to do, and call process.exit.

about 4 years ago · Juan Pablo Isaza Relatório

0

Take a look at the stack attribute from the Error class

class MyError extends Error{
    constructor(message){
        super('');
        this.name = this.constructor.name;
        this.stack= message +" : "+ this.stack //also you can replace this with anything you want

    }
}

throw new MyError("This is my message");

You will then get something like

This is my message : MyError
    at Object.<anonymous> (/home/runner/GummyAdmirableEvents/index.js:10:7)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
about 4 years ago · Juan Pablo Isaza Relatório

0

i'm not sure if this is what you are seeking but you can console out your own error before the standart one like this :

class MyError extends Error {
  constructor(message) {   
      console.error(message)
      super(message);
      this.name = this.constructor.name;
  }   
}
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