Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Created December 11, 2019 14:53
Show Gist options
  • Select an option

  • Save ziadoz/0c4c0e27fb096332f4d086595fda2309 to your computer and use it in GitHub Desktop.

Select an option

Save ziadoz/0c4c0e27fb096332f4d086595fda2309 to your computer and use it in GitHub Desktop.
Log JS Errors via AJAX
// On error send to backend route which will log error into central logging service.
window.onerror = function(message, file, line) {
fetch(
'/log/js-error',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
file: file,
line: line,
message: message,
browser: navigator.userAgent,
url: window.location.href
})
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment