Home > other >  Custom throw new Error message in JavaScript
Custom throw new Error message in JavaScript

Time:03-23

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!

CodePudding user response:

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)

CodePudding user response:

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.

CodePudding user response:

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;
  }   
}
  • Related