Skip to content

Instantly share code, notes, and snippets.

@othiym23
Last active December 23, 2015 20:49
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 othiym23/6692582 to your computer and use it in GitHub Desktop.
Save othiym23/6692582 to your computer and use it in GitHub Desktop.
/**
* Problem:
*
* 1. Connect determines whether middleware functions are error handlers by
* testing their arity. Not cool.
* 2. Downstream Express users rely upon being able to introspect on their
* middleware functions to find specific functions. Sorta less uncool, but
* still a pain.
*
* Solution:
*
* Use eval. This once. For this one specific purpose. Not anywhere else for
* any reason.
*/
function wrapHandle(handle) {
// jshint -W061
var arglist;
// reiterated: testing function arity is stupid
switch (handle.length) {
case 2:
arglist = '(req, res)';
break;
case 3:
arglist = '(req, res, next)';
break;
// don't break other error handlers
case 4:
arglist = '(err, req, res, next)';
break;
default:
arglist = '()';
}
// I am a bad person and this makes me feel bad
var wrapped = eval(
'(function(){return function ' + (handle.name || '') + arglist +
'{return agent.tracer.callbackProxy(handle).apply(this,arguments);};}())'
);
wrapped[ORIGINAL] = handle;
return wrapped;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment