Asdwq Qwksf
Asdwq Qwksf

Reputation: 141

Window.onerror doesn't return info about error

I'm trying to create a custom error handler. I've got the following code to intercept the errors:

window.onerror = function (msg, url, lineNo, columnNo, error) {
            console.log("msg: " + msg + "url: " + url + "lineNo: " + lineNo + "columnNo: " + columnNo);
}

Then later in the code I'm trying to console.log a variable that doesn't exist (in order to trigger the error). In the console I'm getting the custom:

msg: Script error.url: lineNo: 0columnNo: 0

And below that the default:

myjsfile.js:517 Uncaught ReferenceError: xyz is not defined(…)

How can I access this information - filename, line number, error message - and add it to my custom message? Thank you!

Upvotes: 0

Views: 524

Answers (1)

Bimbonkens
Bimbonkens

Reputation: 319

You really should use try-catch https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

create object with all your variables in try and throw it

try {         
    throw your_obj;
}  

and

catch(e)
{
    console.log(e); //will print whatever you made "your_obj" to be
}

Upvotes: 1

Related Questions