3

I'm using an npm module that throws an empty function i.e. UnauthorizedException upon error. I want to be able to identify the function thrown so i can handle it accordingly

function UnauthorizedException() {

}

const f = () => {
  throw (new UnauthorizedException());
};

try {
  f();
} catch (err) {
  // How to identity a function name/property/etc so I can handle the error accordingly
  console.log(err);
  console.log(typeof (err));
}
0

1 Answer 1

3

The UnauthorizedException function is used as constructor. You can check if an object was created from a constructor using the instanceof operator:

function UnauthorizedException() {}

const f = () => {
  throw (new UnauthorizedException());
};

try {
  f();
} catch (err) {
  console.log(err instanceof UnauthorizedException);
}

Sign up to request clarification or add additional context in comments.

2 Comments

If this is the answer, though, surely this is a duplicate of this and a bunch of others?
Sorry I wasn't aware it's a duplicate. Yes that is what I'm looking for i.e. instanceof. Thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.