Created
August 22, 2012 20:28
-
-
Save oyvindkinsey/3429062 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
var mylib = (function() { | |
var api = { | |
provide: function(name, fn) { | |
this[name] = fn; | |
} | |
}; | |
... | |
api.provide('foo', function() { | |
... | |
}); | |
api.provide('bar', function() { | |
... | |
}); | |
return api; | |
})(); |
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
.. | |
function guard(fn) { | |
return function() { | |
try { | |
return fn.apply(this, arguments); | |
} catch (e) { | |
// log error | |
} | |
}; | |
} | |
var api = { | |
provide: function(name, fn) { | |
this[name] = guard(fn); | |
} | |
}; | |
.. |
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
var ManagedError = function(message) { | |
Error.prototype.constructor.apply(this, arguments); | |
this.message = message; | |
}; | |
ManagedError.prototype = new Error(); | |
function guard(fn) { | |
return function() { | |
try { | |
return fn.apply(this, arguments); | |
} catch (e) { | |
if (e instanceof ManagedError) { | |
throw e; | |
} | |
// log error | |
} | |
}; | |
} | |
... | |
api.provide('foo', function(...) { | |
.... | |
throw new ManagedError('Invalid argument'); | |
}); |
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
... | |
function unguard(fn) { | |
return function() { | |
try { | |
return fn.apply(this, arguments); | |
} catch (e) { | |
// surface the error | |
setTimeout(function() { throw e; }, 0); | |
} | |
}; | |
} | |
function guard(fn) { | |
// Helper function for wrapping all functions | |
function wrap(fn, value) { | |
// not all 'function's are actually functions! | |
if (typeof value === 'function' && /^function/.test(value.toString)) { | |
return fn(value); | |
} else if (typeof value === 'object' && value !== null) { | |
for (var key in value) if (value.hasOwnProperty(key)) { | |
value[key] = wrap(fn, value[key]); | |
}; | |
} | |
return value; | |
} | |
return function() { | |
// capture the arguments and unguard any functions | |
var args = Array.prototype.slice.call(arguments) | |
.map(function(arg) { | |
return wrap(unguard, arg); | |
}); | |
try { | |
return fn.apply(this, args); | |
} catch (e) { | |
if (e instanceof ManagedError) { | |
throw e; | |
} | |
// log error | |
} | |
}; | |
} | |
... |
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
function guard(fn) { | |
// Helper function for wrapping all functions | |
function wrap(fn, value) { | |
// not all 'function's are actually functions! | |
if (typeof value === 'function' && /^function/.test(value.toString)) { | |
return fn(value); | |
} else if (typeof value === 'object' && value !== null) { | |
for (var key in value) if (value.hasOwnProperty(key)) { | |
value[key] = wrap(fn, value[key]); | |
}; | |
} | |
return value; | |
} | |
return function() { | |
// capture the arguments and unguard any functions | |
var args = Array.prototype.slice.call(arguments) | |
.map(function(arg) { | |
return wrap(unguard, arg); | |
}); | |
try { | |
return wrap(unguard, fn.apply(this, args)); | |
} catch (e) { | |
if (e instanceof ManagedError) { | |
throw e; | |
} | |
// log error | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment