Last active
September 10, 2015 12:31
-
-
Save nbubna/65fa17edef6a3c09b918 to your computer and use it in GitHub Desktop.
A convenient global error handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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