Home » Nodejs » How do I get a correct backtrace for a custom error class in NodeJS?

How do I get a correct backtrace for a custom error class in NodeJS?

Posted by: admin November 30, 2017 Leave a comment


My understanding for the “right” way to make a custom Error class in JavaScript is something like this:

function MyError(message) {  
    this.name = "MyError";  
    this.message = message || "Default Message";  
MyError.prototype = new Error();  
MyError.prototype.constructor = MyError;

(Code snippet mooked from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error.)

With NodeJS, if I try to check for an error of this type like:

var err = new MyError("whoops");

…the backtrace will show the context of the Error object I created at compile time to be the prototype for MyError, not the MyError object I created with “new MyError()”.

Is there some way that I can get the correct backtrace data for the actual error, rather than the prototype?


We need to invoke the super function – captureStackTrace

var util = require('util');

function MyError(message) {
  Error.call(this); //super constructor
  Error.captureStackTrace(this, this.constructor); //super helper method to include stack trace in error object

  this.name = this.constructor.name; //set our function’s name as error name.
  this.message = message; //set the error message

// inherit from Error
util.inherits(MyError, Error);


You can use this node module to extend Error types easily


@Jay Kumar, has one good answer here. However, maybe here is another similar solution here

module.exports = function CustomError(message, extra) {
  Error.captureStackTrace(this, this.constructor);
  this.name = this.constructor.name;
  this.message = message;
  this.extra = extra;

require('util').inherits(module.exports, Error);

Error.call(this) – creates another error object (wasting a bunch of time) and doesn’t touch this at all

Since the ECMAScript6 could be supported in the latest Node.js version. The answer under ES6 could be refer to this link.

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

Here are test codes under Node v4.2.1

class MyError extends Error{
        constructor(msg, extra) {
                this.message = msg;
                this.name = 'MyError';
                this.extra = extra;

var myerr = new MyError("test", 13);


MyError: test
    at MyError (/home/bsadmin/test/test.js:5:8)
    at Object.<anonymous> (/home/bsadmin/test/test.js:12:13)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3
{ [MyError: test] name: 'MyError', extra: 13 }