Skip to content

Commit 770f11b

Browse files
authored
Merge pull request #1790 from mitsos1os/return-promise-on-error
Convert error thrown by a property setter to a rejected promise
2 parents 89a964e + b30fbf8 commit 770f11b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/dao.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ DataAccessObject.create = function(data, options, cb) {
334334
this.applyProperties(enforced, obj);
335335
obj.setAttributes(enforced);
336336
} catch (err) {
337-
return cb(err);
337+
process.nextTick(function() {
338+
cb(err);
339+
});
340+
return cb.promise;
338341
}
339342

340343
Model = this.lookupModel(data); // data-specific
@@ -2606,7 +2609,10 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
26062609
this.applyProperties(enforced, inst);
26072610
inst.setAttributes(enforced);
26082611
} catch (err) {
2609-
return cb(err);
2612+
process.nextTick(function() {
2613+
cb(err);
2614+
});
2615+
return cb.promise;
26102616
}
26112617

26122618
Model = this.lookupModel(data); // data-specific

test/manipulation.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const ValidationError = require('..').ValidationError;
1717

1818
const UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
1919

20+
const throwingSetter = (value) => {
21+
if (!value) return; // no-op
22+
throw new Error('Intentional error triggered from a property setter');
23+
};
24+
2025
describe('manipulation', function() {
2126
before(function(done) {
2227
db = getSchema();
@@ -28,8 +33,11 @@ describe('manipulation', function() {
2833
age: {type: Number, index: true},
2934
dob: Date,
3035
createdAt: {type: Date, default: Date},
36+
throwingSetter: {type: String, default: null},
3137
}, {forceId: true, strict: true});
3238

39+
Person.setter.throwingSetter = throwingSetter;
40+
3341
db.automigrate(['Person'], done);
3442
});
3543

@@ -111,6 +119,11 @@ describe('manipulation', function() {
111119
.catch(done);
112120
});
113121

122+
it('should return rejected promise when model initialization failed', async () => {
123+
await Person.create({name: 'Sad Fail', age: 25, throwingSetter: 'something'}).should
124+
.be.rejectedWith('Intentional error triggered from a property setter');
125+
});
126+
114127
it('should instantiate an object', function(done) {
115128
const p = new Person({name: 'Anatoliy'});
116129
p.name.should.equal('Anatoliy');
@@ -1563,7 +1576,9 @@ describe('manipulation', function() {
15631576
Post = db.define('Post', {
15641577
title: {type: String, length: 255},
15651578
content: {type: String},
1579+
throwingSetter: {type: String, default: null},
15661580
}, {forceId: true});
1581+
Post.setter.throwingSetter = throwingSetter;
15671582
db.automigrate('Post', done);
15681583
});
15691584

@@ -1598,11 +1613,19 @@ describe('manipulation', function() {
15981613
id: created.id,
15991614
title: 'Draft',
16001615
content: 'a content',
1616+
throwingSetter: null,
16011617
});
16021618

16031619
// Verify that no warnings were triggered
16041620
Object.keys(Post._warned).should.be.empty();
16051621
});
1622+
1623+
it('should return rejected promise when model initialization failed', async () => {
1624+
const firstNotFailedPost = await Post.create({title: 'Sad Post'}); // no property with failing setter
1625+
await Post.replaceById(firstNotFailedPost.id, {
1626+
title: 'Sad Post', throwingSetter: 'somethingElse',
1627+
}).should.be.rejectedWith('Intentional error triggered from a property setter');
1628+
});
16061629
});
16071630

16081631
describe('findOrCreate', function() {

0 commit comments

Comments
 (0)