-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Post validate error handler does not fire #4885
Comments
Thanks for the repro script. Looks like it also doesn't run when you actually make a db operation:
|
Looks like post save error handler does not fire either. Do error handlers ever fire at all? |
@varunjayaraman |
yep, sorry about that, not sure what happened |
@vkarpov15 |
What do you mean by "post save error handler not intercepting errors"? I'm not aware of such an issue. |
@vkarpov15 The test case is the same, but |
@sshymko works just fine: const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-4885`;
mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);
let PaymentSchema = new mongoose.Schema({
currency: { type: String, required: true, enum: ['USD'], default: 'USD' },
amount: { type: Number, required: true },
});
PaymentSchema.post('save', function(error, doc, next) {
console.log(`Error in post validate hook ${error}`)
next(new Error('Invalid payment information'));
});
let PaymentModel = mongoose.model('Payment', PaymentSchema);
try {
let payment = new PaymentModel({
currency: 'EUR',
amount: 10,
});
console.log('saving...');
payment.save()
.then(doc => {
console.log('saved', doc);
process.exit(0);
})
.catch(error => {
console.error(`Error in catch handler ${error}\n${error.stack}`);
process.exit(2);
})
} catch (error) {
console.error(error);
process.exit(2);
} Output
Which version of mongoose are you using? |
@vkarpov15 |
@vkarpov15 |
@sshymko can you please clarify with a code sample? |
This error is still there in mongoose v5.9.5.
Check the outputs of both console.log. They are different. |
@jayadrathamondal we unfortunately don't have a way to run error handling middleware on |
Environment:
Steps to reproduce:
Expected result:
validate
andvalidateSync
return converted errorActual result:
The text was updated successfully, but these errors were encountered: