Skip to content

Instantly share code, notes, and snippets.

@lsauer
Created September 23, 2014 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsauer/4043c4bbb9cd00281696 to your computer and use it in GitHub Desktop.
Save lsauer/4043c4bbb9cd00281696 to your computer and use it in GitHub Desktop.
Javascript: Lean Exception handler
/**
* lsauer, 2013
* Lean handler for exception reporting; stdout is set by default to console.log
* @param {exc} exc an Exception instance of Error, containing a message and stack property
* @param {exc} stdOut A variadic function for error reporting. By default: console.log
* @return undefined
*/
var exceptionHandler = function(exc, stdOut){
var stdOut = stdOut || function() { console.log.apply(console, arguments) };
if(stdOut instanceof Function){
if(exc instanceof Error){
stdOut('Exception:', exc.message, '; Stack:', exc.stack, '; Scope:', this.toString());
}else{
stdOut('Exception missing. Argument:', exc);
}
}else{
if(exc instanceof Error){
exceptionHandler._log.push('Exception:' + exc.message +';' + exc.stack + '; Argument: '+ stdOut.toString());
}else{
exceptionHandler._log.push('Exception missing.; Argument(s): '+ exc.toString() + ', ' + stdOut.toString());
}
}
};
/** @var {array} _log internal log history for custom functionality */
exceptionHandler._log = [];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment