Asdwq Qwksf Asdwq Qwksf - 1 year ago 97
jQuery Question

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!

Answer Source

You really should use try-catch

create object with all your variables in try and throw it

try {         
    throw your_obj;


    console.log(e); //will print whatever you made "your_obj" to be
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download