mongoose
mongoose copied to clipboard
Pre-save hook silently swallows exception
It's a bug
What is the current behavior? pre-save hook silently swallows the exceptions when we don't pass callback to function but according to documentation it shouldn't be a problem: https://mongoosejs.com/docs/middleware.html#error-handling
If the current behavior is a bug, please provide the steps to reproduce.
const Schema = mongoose.Schema;
describe('PreSave', function () {
it('test pre save', async function () {
const TestScheme = new Schema({name: String});
TestScheme.pre('save', function () {
throw new Error('test err');
});
const TestModel = mongoose.model('testModel', TestScheme);
const testObject = new TestModel({name: 't'});
try {
await testObject.save();
} catch (e) {
// doesn't reach here!!
}
});
});
What is the expected behavior? We must see the thrown error in catch section.
Node.js: 12.8.3 Mongoose: 5.11.4 MongoDB: 4.0.14