Skip to content

Instantly share code, notes, and snippets.

@rianby64
Created October 7, 2016 12:54
Show Gist options
  • Save rianby64/3b945dd8dbf308847a6f059c670dca62 to your computer and use it in GitHub Desktop.
Save rianby64/3b945dd8dbf308847a6f059c670dca62 to your computer and use it in GitHub Desktop.
A simple demo that shows how to catch all errors through the window.onerror (via addEventListener)
<!doctype html>
<html>
<body>
<button>Fire error</button>
<script>
'use script';
window.addEventListener('error', e => {
console.error('catch error', e);
});
document.querySelector('button').addEventListener('click', e => {
var message = 'this is the message error';
var error = {
message: message,
filename: 'this is the filename',
colno: 4,
lineno: 5,
error: new Error(message)
};
window.dispatchEvent(new ErrorEvent('error', error));
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment