Skip to content

Instantly share code, notes, and snippets.

@nbubna
Last active September 10, 2015 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nbubna/65fa17edef6a3c09b918 to your computer and use it in GitHub Desktop.
Save nbubna/65fa17edef6a3c09b918 to your computer and use it in GitHub Desktop.
A convenient global error handler
(function(scope, console, CustomEvent) {
var error = scope.error = function() {
var output = []
for (var i=0; i<arguments.length; i++) {
var arg = arguments[i];
if (typeof arg === "object" && arg.length) {
output.push.apply(output, Array.prototype.slice.call(arg));
} else {
output.push(arg);
}
}
if (typeof error.target === "function") {
error.target.apply(scope, output);
} else if (scope.alert) {
scope.alert('Error: '+output.join(', '));
}
if (CustomEvent && typeof scope.dispatchEvent === "function") {
if (output.length === 1) {
output = output[0];
}
scope.dispatchEvent(new CustomEvent("error", {
detail: output
}));
}
};
error.target = console ? (console.error||console.log).bind(console) : null;
})(this, this.console, this.CustomEvent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment