return failed promise on error (cherry-picked)

This commit is contained in:
Dimitris Xalatsis 2019-11-02 10:39:37 +02:00 committed by Dimitris
parent 6ed60653eb
commit 315545daa0
2 changed files with 30 additions and 2 deletions

View File

@ -334,7 +334,10 @@ DataAccessObject.create = function(data, options, cb) {
this.applyProperties(enforced, obj);
obj.setAttributes(enforced);
} catch (err) {
return cb(err);
process.nextTick(function() {
cb(err);
});
return cb.promise;
}
Model = this.lookupModel(data); // data-specific
@ -2631,7 +2634,10 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
this.applyProperties(enforced, inst);
inst.setAttributes(enforced);
} catch (err) {
return cb(err);
process.nextTick(function() {
cb(err);
});
return cb.promise;
}
Model = this.lookupModel(data); // data-specific

View File

@ -17,6 +17,11 @@ const ValidationError = require('..').ValidationError;
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;
const throwingSetter = (value) => {
if (!value) return; // no-op
throw new Error('Intentional error triggered from a property setter');
};
describe('manipulation', function() {
before(function(done) {
db = getSchema();
@ -28,8 +33,11 @@ describe('manipulation', function() {
age: {type: Number, index: true},
dob: Date,
createdAt: {type: Date, default: Date},
throwingSetter: {type: String, default: null},
}, {forceId: true, strict: true});
Person.setter.throwingSetter = throwingSetter;
db.automigrate(['Person'], done);
});
@ -111,6 +119,11 @@ describe('manipulation', function() {
.catch(done);
});
it('should return rejected promise when model initialization failed', async () => {
await Person.create({name: 'Sad Fail', age: 25, throwingSetter: 'something'}).should
.be.rejectedWith('Intentional error triggered from a property setter');
});
it('should instantiate an object', function(done) {
const p = new Person({name: 'Anatoliy'});
p.name.should.equal('Anatoliy');
@ -1539,7 +1552,9 @@ describe('manipulation', function() {
Post = db.define('Post', {
title: {type: String, length: 255},
content: {type: String},
throwingSetter: {type: String, default: null},
}, {forceId: true});
Post.setter.throwingSetter = throwingSetter;
db.automigrate('Post', done);
});
@ -1552,6 +1567,13 @@ describe('manipulation', function() {
done();
});
});
it('should return rejected promise when model initialization failed', async () => {
const firstNotFailedPost = await Post.create({title: 'Sad Post'}); // no property with failing setter
await Post.replaceById(firstNotFailedPost.id, {
title: 'Sad Post', throwingSetter: 'somethingElse',
}).should.be.rejectedWith('Intentional error triggered from a property setter');
});
});
describe('findOrCreate', function() {