npmecmascript-695% confidence\u2191 140

Extending Error in Javascript with ES6 syntax & Babel

Full error message
I am trying to extend Error with ES6 and Babel. It isn't working out.

class MyError extends Error {
  constructor(m) {
    super(m);
  }
}

var error = new Error("ll");
var myerror = new MyError("ll");
console.log(error.message) //shows up correctly
console.log(myerror.message) //shows empty string

The Error object never get the right message set.

Try in Babel REPL.

Now I have seen a few solutions on SO (for example here), but they all seem very un-ES6-y. How to do it in a nice, ES6 way? (That is working in Babel)

Based on Karel Bílek's answer, I'd make a small change to the constructor: class ExtendableError extends Error { constructor(message) { super(message); this.name = this.constructor.name; if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, this.constructor); } else { this.stack = (new Error(message)).stack; } } } // now I can extend class MyError extends ExtendableError {} var myerror = new MyError("ll"); console.log(myerror.message); console.log(myerror instanceof Error); console.log(myerror.name); console.log(myerror.stack); This will print MyError in the stack, and not the generic Error. It will also add the error message to the stack trace - which was missing from Karel's example. It will also use captureStackTrace if it's available. With Babel 6, you need transform-builtin-extend (npm) for this to work.

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/b61fc31922be84e6c5e41ce1e4a9e9f3c1b69d6e093e67e931e4bce9c617e7aa
hash \u00b7 b61fc31922be84e6c5e41ce1e4a9e9f3c1b69d6e093e67e931e4bce9c617e7aa
Extending Error in Javascript with ES6 syntax & Babel — DepScope fix | DepScope