Skip to content

Commit 9e3427a

Browse files
committed
fix(document): catch sync errors in document pre hooks and report as error
Fix #5738
1 parent 1d87987 commit 9e3427a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"async": "2.1.4",
2323
"bson": "~1.0.4",
24-
"hooks-fixed": "2.0.0",
24+
"hooks-fixed": "2.0.2",
2525
"kareem": "1.5.0",
2626
"mongodb": "2.2.33",
2727
"mpath": "0.3.0",

test/document.hooks.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,26 @@ describe('document: hooks:', function() {
763763
done();
764764
});
765765

766+
it('sync exceptions get passed as errors (gh-5738)', function(done) {
767+
var bookSchema = new Schema({ title: String });
768+
769+
/* eslint-disable no-unused-vars */
770+
bookSchema.pre('save', function(next) {
771+
throw new Error('woops!');
772+
});
773+
774+
var Book = mongoose.model('gh5738', bookSchema);
775+
776+
var book = new Book({});
777+
778+
book.title = 'Professional AngularJS';
779+
book.save(function(error) {
780+
assert.ok(error);
781+
assert.equal(error.message, 'woops!');
782+
done();
783+
});
784+
});
785+
766786
it('nested subdocs only fire once (gh-3281)', function(done) {
767787
var L3Schema = new Schema({
768788
title: String

0 commit comments

Comments
 (0)