Skip to content

mongoose plugin requires first argument of "next" be type of Error #2441

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

Closed
mocheng opened this issue Nov 5, 2014 · 6 comments
Closed

mongoose plugin requires first argument of "next" be type of Error #2441

mocheng opened this issue Nov 5, 2014 · 6 comments
Milestone

Comments

@mocheng
Copy link
Contributor

mocheng commented Nov 5, 2014

It cost me hours to figure out bug in below code.

calendarSchema.pre('save' function(next) {
    if (this.begin_time > this.end_time) {
       return next('begin time should be earlier than end time');
   }

   next();
});

If next is invoked with first argument as object not in Error type, it just silently fail without trigger callback of save.

I believe this should be improved. next can transfer first argument into Error if it is string or else.

@vkarpov15 vkarpov15 added this to the 3.8.20 milestone Nov 5, 2014
@vkarpov15
Copy link
Collaborator

Thanks for heads up, sorry finding this bug was such a hassle

@yads
Copy link

yads commented Nov 19, 2014

I was trying to figure out why it was silently failing. Thanks for this.

@alabid
Copy link
Contributor

alabid commented Dec 1, 2014

@yads @mocheng It turns out that this is not a bug but how the hooks npm package is supposed to work. Nevertheless, I agree that this behavior is not straight-forward. Mongoose depends on that package for defining pre and post hooks for validation, saving, and so on.

According to the documentation on the hooks docs page, next(<some string>) should pass <some string> to the next middleware whereas next(new Error(<some string>)) should invoke the error callback. I've written a test here to show that mongoose correctly models/uses the behavior of the hooks package.

vkarpov15 added a commit that referenced this issue Dec 2, 2014
added tests to verify that passing error and non-error messages through ...
@vkarpov15 vkarpov15 modified the milestones: 3.8.21, 3.8.20 Dec 5, 2014
@vkarpov15 vkarpov15 modified the milestones: 3.8.22, 3.8.21 Dec 18, 2014
@vkarpov15 vkarpov15 removed this from the 3.8.22 milestone Dec 18, 2014
@yads
Copy link

yads commented Dec 18, 2014

That's very interesting and definitely a non standard way to use middleware. Thanks for looking into it.

@vkarpov15
Copy link
Collaborator

Yeah I agree this is a little bit weird. I'd be open to a discussion to change this behavior.

@vkarpov15
Copy link
Collaborator

This is fixed in 3.9.x Not quite fixed, see #3672.

@vkarpov15 vkarpov15 added this to the 4.0 milestone Jan 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants