Skip to content

Instantly share code, notes, and snippets.

@karlwestin
Created July 31, 2014 09:22
Show Gist options
  • Save karlwestin/bb9d08bec65e14f44272 to your computer and use it in GitHub Desktop.
Save karlwestin/bb9d08bec65e14f44272 to your computer and use it in GitHub Desktop.
simple window.onerror handler
function errorHandler(msg, url, line, column, err) {
// firefox bug to watch for 5 arg error handler
// https://bugzilla.mozilla.org/show_bug.cgi?id=630464
/*
* most browsers don't give column number,
* but chrome can do it sometimes
*/
if(typeof column == "object") {
err = column;
}
if(typeof column !== "number") {
column = undefined;
}
var event = {
event: "error",
name: "jsError",
data: {
url: url || window.location.href,
msg: (err && err.message) || msg,
line: line,
column: column,
useragent: navigator.userAgent,
stack: err && err.stack
}
};
// Here you'd have to use whatever you have
// to post to your logging server
writeToMyErrorLogger(event);
}
window.onError = errorHandler;
/*
Important configuration instructions:
To get good error messages, including stacks and so
your JS files from CDN needs to be served with an appropirate Access-Control-Allow-Origin header,
AND you need to use the attribtue crossorigin="anonymous" on all the script tags loading scripts
that should be reported
if you don't use CDN you don't need to care
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment