Last active
December 23, 2015 20:49
-
-
Save othiym23/6692582 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
/** | |
* 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