Created
May 9, 2011 19:25
-
-
Save esamattis/963200 to your computer and use it in GitHub Desktop.
Try to desperately to capture exceptions and log them to the console.
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
// 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); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to capture rest of the exceptions?