-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asyncUtilWrap
expects the next
argument to be the last, which is incorrect when there are additional arguments
#28
Comments
asyncUtilWrap()
expects next argument to be the last, which doesn't work when there are additional arguments asyncUtilWrap
expects the next
argument to be the last, which is incorrect when there are additional arguments
Anyone? |
Why would your express middleware arguments ever have anything after the
Where are you even using this library? |
I personally am not using a middleware signature with arguments after There two pieces of evidence that suggest it is a supported scenario:
app.param('user', function(req, res, next, id) {
// try to get the user details from the User model and attach it to the request object
User.find(id, function(err, user) {
if (err) {
next(err);
} else if (user) {
req.user = user;
next();
} else {
next(new Error('failed to load user'));
}
});
}); |
As far as I know,
I noticed that too 🤔 It was added by one of the contributors, but I don't think it was intentional (a208f5f)
Well, we better not |
- Uses a `function` type check to identify `next` argument - Test fix Abazhenov#28
I've just faced this issue when using this function for middleware in a parameter handler. The parameter name is the last argument then. |
@robertbullen here's a fix to correctly get the next argument: exports.handleAsyncErrors = fn => function AsyncErrorsWrapper(...args) {
// next is always the last function of the arguments passed to an express middleware or view
const getNext = args => [...args].reverse().find(arg => typeof arg === 'function')
const next = getNext(args)
return fn(...args).catch(next)
} |
@robertbullen @artursudnik Hi from 2021! :-) If there are extra parameters, I wonder how Express distinguishes e.g. |
BTW, it's worth assigning |
Apparently, it does not have to. |
The unit test 'should provide additional arguments to the middleware' implies that arguments following the
next
callback are allowed. ButasyncUtilWrap
always looks fornext
as the last argument. Thus, in a scenario where additional arguments are provided and an error is thrown,next
will not be properly called with the error.The text was updated successfully, but these errors were encountered: