Create a gist now

Instantly share code, notes, and snippets.

Try to desperately to capture exceptions and log them to the console.
// http://stackoverflow.com/questions/5931033/how-can-i-know-if-a-javascript-exception-occurred-in-a-phonegap-application-and
(function(){
window.onexception = function(e) {
console.log("Got an exception ", e, e.stack);
};
/**
* Wrap function with exception caturer.
* */
var captureExceptions = function(origFn) {
return function () {
try {
origFn.apply(this, arguments);
}
catch (e) {
window.onexception(e);
throw e;
}
};
};
/**
* Wraps callbacks in object methods
*
* */
var wrapCallbackFunction = function(obj, fnName, callbackIndex) {
var origFn = obj[fnName];
obj[fnName] = function() {
var args = [].slice.call(arguments);
args[callbackIndex] = captureExceptions(args[callbackIndex]);
origFn.apply(this, args);
};
};
wrapCallbackFunction(window, "addEventListener", 1);
wrapCallbackFunction(Node.prototype, "addEventListener", 1);
wrapCallbackFunction(window, "setTimeout", 0);
wrapCallbackFunction(window, "setInterval", 0);
}());
@epeli
Owner

How to capture rest of the exceptions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment