Skip to content

Instantly share code, notes, and snippets.

@esamattis
Created May 9, 2011 19:25
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 esamattis/963200 to your computer and use it in GitHub Desktop.
Save esamattis/963200 to your computer and use it in GitHub Desktop.
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);
}());
@esamattis
Copy link
Author

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