CodingIntrigue
CodingIntrigue

Reputation: 78535

Retrieve type of error

I'm writing a handler for my client-side errors using the onerror event. What I'm really interested in is the type of Error thrown, as a string.

How can I find this, given the errorObject in the event handler?

window.onerror = function(errorMsg, url, lineNumber, colNumber, errorObject) {
    // ...
};

jsFiddle

typeof - just returns object. I could do instanceof and hardcode the list of error types from the MDN docs, but that doesn't seem right.

Upvotes: 1

Views: 150

Answers (1)

Joe
Joe

Reputation: 15802

You can get the type of the error as a string with obj.name, giving you "TypeError" - jsFiddle.

Upvotes: 3

Related Questions