Last active
December 10, 2015 11:48
-
-
Save devinrhode2/4429848 to your computer and use it in GitHub Desktop.
This file contains 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
//THIS IS NOW SHIELD.JS github.com/devinrhode2/shield.js | |
/** | |
* (function(){ | |
* ..code.. | |
* }).runInTryCatch(); | |
* Shorthand to run function in a TraceKit try/catch block. | |
* | |
* Prototype is less loud than: | |
* runInTryCatch(function(){ | |
* ..code.. | |
* }) | |
*/ | |
TraceKit.runInTryCatch = function TraceKit_runInTryCatchFn(fn) { | |
try { | |
fn(); | |
} catch (e) { | |
TraceKit.report(e); | |
} | |
}; | |
(function TraceKitWrapper() { | |
//_.isFunction, restructured some | |
var isFunction = (typeof (/./) !== 'function' ? | |
//use optimized version: | |
function(obj) { | |
return typeof (obj) === 'function'; | |
} | |
: | |
function(obj) { | |
return toString.call(obj) == '[object Function]'; | |
} | |
); | |
/** | |
* TraceKit.apiWrapper | |
* Mutates an api so every functon argument passed in get's mutated and wrapped in a try/catch block. | |
* | |
* Example: catch all $(document).ready(): | |
* $.fn.on = TraceKit.apiWrapper($.fn.on); | |
*/ | |
TraceKit.apiWrapper = function apiWrapperFn(apiFn) { | |
return function apiWrapper_ReturnedFn() { | |
var args = [].slice.call(arguments, 0); | |
var length = args.length; | |
var arg; | |
//before executing the overriden function, transform each function arg to have a try/catch wrapper | |
while (arg = args[--length]) { | |
if (isFunction(arg)) { | |
prevFunc = arg; | |
arg = function TraceKitApiWrapper() { | |
try { | |
var args = [].slice.call(arguments, 0); | |
if (prevFunc.apply) { | |
prevFunc.apply(this, args); | |
} else { //setTimeout/setInterval in IE | |
prevFunc(args[0], args[1]); | |
} | |
} catch (e) { | |
TraceKit.report(e); | |
} | |
}; | |
} | |
} | |
//errors in apiFn bubble up (to window or hopefully a try/catch block.) | |
apiFn.apply(this, args); | |
}; | |
}; | |
}).runInTryCatch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment