Merge pull request #1800 from mitsos1os/backport-1790

Return rejected promise on error
This commit is contained in:
Miroslav Bajtoš 2019-12-05 16:23:05 +01:00 committed by GitHub
commit bcd663b75f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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() {