Skip to content

Commit 1ef8274

Browse files
committedDec 8, 2020
fix(middleware): ensure sync errors in pre hooks always bubble up to the calling code
Fix #9659
1 parent 0e2058d commit 1ef8274

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"@types/mongodb": "^3.5.27",
2323
"bson": "^1.1.4",
24-
"kareem": "2.3.1",
24+
"kareem": "2.3.2",
2525
"mongodb": "3.6.3",
2626
"mongoose-legacy-pluralize": "1.0.2",
2727
"mpath": "0.8.0",

‎test/document.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -9788,4 +9788,21 @@ describe('document', function() {
97889788
assert.ok(documentFromDefault === user);
97899789
assert.equal(documentFromDefault.name, 'Hafez');
97909790
});
9791+
9792+
it('handles pre hook throwing a sync error (gh-9659)', function() {
9793+
const TestSchema = new Schema({ name: String });
9794+
9795+
TestSchema.pre('save', function() {
9796+
throw new Error('test err');
9797+
});
9798+
const TestModel = db.model('Test', TestSchema);
9799+
9800+
return co(function*() {
9801+
const testObject = new TestModel({ name: 't' });
9802+
9803+
const err = yield testObject.save().then(() => null, err => err);
9804+
assert.ok(err);
9805+
assert.equal(err.message, 'test err');
9806+
});
9807+
});
97919808
});

‎test/model.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1603,14 +1603,14 @@ describe('Model', function() {
16031603
let docMiddleware = 0;
16041604
let queryMiddleware = 0;
16051605

1606-
schema.pre('remove', { query: true }, function() {
1607-
assert.ok(this instanceof Model.Query);
1606+
schema.pre('remove', { query: true, document: false }, function() {
16081607
++queryMiddleware;
1608+
assert.ok(this instanceof Model.Query);
16091609
});
16101610

1611-
schema.pre('remove', { document: true }, function() {
1612-
assert.ok(this instanceof Model);
1611+
schema.pre('remove', { query: false, document: true }, function() {
16131612
++docMiddleware;
1613+
assert.ok(this instanceof Model);
16141614
});
16151615

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

0 commit comments

Comments
 (0)
Please sign in to comment.