1

I have a code structure like this:

class CustomError {
  constructor(status, error = null, message = null) {
    this.status = status;
    this.error = error;
    this.message = message;
  }
}

const foo = async function() {
  throw new CustomError('foo error');
}

const bar = async function() {
  try {
    foo();
  } catch (error) {
    if (error instanceof CustomError) {
      throw error;
    }
    throw new CustomError('bar error');
  }
}

bar();

This throws bar error.

I placed this before the if statement: console.log(error instanceof CustomError, error) which logs:

false
CustomError {status: "foo", message: null, error: null}

this seems contradictory. If it logs error as CustomError shouldn't error instanceof CustomError return true?

The E
  • 697
  • 1
  • 9
  • 23
  • Does this answer your question? [How do I create a custom Error in JavaScript?](https://stackoverflow.com/questions/783818/how-do-i-create-a-custom-error-in-javascript) – Renat Mar 16 '20 at 14:10

0 Answers0