Reputation: 92935
I have following code to capture errors in javascript
<script type="text/javascript">
window.onerror = function(msg, url, linenumber) {
console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
return true;
}
</script>
I opened chrome developer console and going to generate ReferenceError
by random javascript code. Lets say:
person.run(); // person object does not exist
It throws Uncaught ReferenceError: person is not defined
and printed in the console. But it is not captured by window.onerror
. Why?
Upvotes: 7
Views: 8027
Reputation: 432
For console or for other browser app
document.onerror=function (errorMsg, url, lineNumber,colNumder,error){
console.log('OnError');
return true
}
function consoleErrorTrap(call){
try{
return call();
} catch(e){
let event= new ErrorEvent('error',{error:e})
document.dispatchEvent(event);
}
}
consoleErrorTrap(()=>{
throw new Error()
})
Upvotes: 0
Reputation: 530
An event handler for runtime script errors.
Note that some/many error events do not trigger window.onerror, you have to listen for them specifically.
open this link this is use full for you:
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onerror
Upvotes: 3