I want to throw some things in my JS code and I want them to be instanceof Error, but I also want to have them be something else.

In Python, typically, one would subclass Exception.

What’s the appropriate thing to do in JS?

27 Answers
27

In ES6:

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

source

Leave a Reply

Your email address will not be published. Required fields are marked *