UNPKG

catch-all-errors

Version:

Catch all JavaScript errors and post them to your server

46 lines (41 loc) 1.25 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="width=device-width, initial-scale=1" name="viewport"> </head> <body> <script> window.onload = function() { var el = document.getElementById("testInput"); var barf = function() { throw new Error('borked'); }; if (el.addEventListener) { el.addEventListener('change', barf, true); } else { el.attachEvent('onchange', barf); } } window.onerror = function(message, url, line, column, error) { console.log("onerror", arguments); var e = { "message": message, "url": url, "pos": [line, column], "useragent": navigator.userAgent, }; if (error.stack) e.stack = error.stack; if (error.fileName) e.file = error.fileName; if (error.message) e.message = error.message; console.log(e); alert(JSON.stringify(e, null, 2)); } </script> <p> Type something into the text field and hit [Enter]. You should get an alert showing the arguments passed to window.onerror(). </p> <input type="text" id="testInput" name="testInput"> </body> </html>