Skip to content

Commit

Permalink
fix(middleware): ensure sync errors in pre hooks always bubble up to …
Browse files Browse the repository at this point in the history
…the calling code

Fix #9659
  • Loading branch information
vkarpov15 committed Dec 8, 2020
1 parent 0e2058d commit 1ef8274
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@types/mongodb": "^3.5.27",
"bson": "^1.1.4",
"kareem": "2.3.1",
"kareem": "2.3.2",
"mongodb": "3.6.3",
"mongoose-legacy-pluralize": "1.0.2",
"mpath": "0.8.0",
Expand Down
17 changes: 17 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9788,4 +9788,21 @@ describe('document', function() {
assert.ok(documentFromDefault === user);
assert.equal(documentFromDefault.name, 'Hafez');
});

it('handles pre hook throwing a sync error (gh-9659)', function() {
const TestSchema = new Schema({ name: String });

TestSchema.pre('save', function() {
throw new Error('test err');
});
const TestModel = db.model('Test', TestSchema);

return co(function*() {
const testObject = new TestModel({ name: 't' });

const err = yield testObject.save().then(() => null, err => err);
assert.ok(err);
assert.equal(err.message, 'test err');
});
});
});
8 changes: 4 additions & 4 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1603,14 +1603,14 @@ describe('Model', function() {
let docMiddleware = 0;
let queryMiddleware = 0;

schema.pre('remove', { query: true }, function() {
assert.ok(this instanceof Model.Query);
schema.pre('remove', { query: true, document: false }, function() {
++queryMiddleware;
assert.ok(this instanceof Model.Query);
});

schema.pre('remove', { document: true }, function() {
assert.ok(this instanceof Model);
schema.pre('remove', { query: false, document: true }, function() {
++docMiddleware;
assert.ok(this instanceof Model);
});

const Model = db.model('Test', schema);
Expand Down

0 comments on commit 1ef8274

Please sign in to comment.