Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!