Some browsers now send the actual error object. Not sure which browsers support this. It says that Gecko 31 is required. I don’t know how chrome and others are.
You can create a custom error, for example:
Magic = function( message ) { this.name = 'Magic'; this.message = message; } Magic.prototype = new Error(); Magic.prototype.constructor = Magic;
throw it somewhere:
throw new Magic('Kaboom');
and catch it in window.onerror as follows:
window.onerror = function ( message, filename, lineno, colno, error ){ if ( error !== undefined && error.hasOwnProperty( "name" ) && error.name == "Magic"){ alert("some uncaught magic caused: " + message +" - in "+filename +"("+lineno+")" ); return true; } }
check fiddle **
you can also check if the message contains “Magic”, but then you can accidentally catch other errors containing “Magic”.
ps: sorry for necro, I stumbled upon this search of the same .. so I thought I could add an answer.
source share