return failed promise on error (cherry-picked)
This commit is contained in:
parent
6ed60653eb
commit
315545daa0
10
lib/dao.js
10
lib/dao.js
|
@ -334,7 +334,10 @@ DataAccessObject.create = function(data, options, cb) {
|
||||||
this.applyProperties(enforced, obj);
|
this.applyProperties(enforced, obj);
|
||||||
obj.setAttributes(enforced);
|
obj.setAttributes(enforced);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb(err);
|
process.nextTick(function() {
|
||||||
|
cb(err);
|
||||||
|
});
|
||||||
|
return cb.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
Model = this.lookupModel(data); // data-specific
|
Model = this.lookupModel(data); // data-specific
|
||||||
|
@ -2631,7 +2634,10 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
|
||||||
this.applyProperties(enforced, inst);
|
this.applyProperties(enforced, inst);
|
||||||
inst.setAttributes(enforced);
|
inst.setAttributes(enforced);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb(err);
|
process.nextTick(function() {
|
||||||
|
cb(err);
|
||||||
|
});
|
||||||
|
return cb.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
Model = this.lookupModel(data); // data-specific
|
Model = this.lookupModel(data); // data-specific
|
||||||
|
|
|
@ -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 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() {
|
describe('manipulation', function() {
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
|
@ -28,8 +33,11 @@ describe('manipulation', function() {
|
||||||
age: {type: Number, index: true},
|
age: {type: Number, index: true},
|
||||||
dob: Date,
|
dob: Date,
|
||||||
createdAt: {type: Date, default: Date},
|
createdAt: {type: Date, default: Date},
|
||||||
|
throwingSetter: {type: String, default: null},
|
||||||
}, {forceId: true, strict: true});
|
}, {forceId: true, strict: true});
|
||||||
|
|
||||||
|
Person.setter.throwingSetter = throwingSetter;
|
||||||
|
|
||||||
db.automigrate(['Person'], done);
|
db.automigrate(['Person'], done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -111,6 +119,11 @@ describe('manipulation', function() {
|
||||||
.catch(done);
|
.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) {
|
it('should instantiate an object', function(done) {
|
||||||
const p = new Person({name: 'Anatoliy'});
|
const p = new Person({name: 'Anatoliy'});
|
||||||
p.name.should.equal('Anatoliy');
|
p.name.should.equal('Anatoliy');
|
||||||
|
@ -1539,7 +1552,9 @@ describe('manipulation', function() {
|
||||||
Post = db.define('Post', {
|
Post = db.define('Post', {
|
||||||
title: {type: String, length: 255},
|
title: {type: String, length: 255},
|
||||||
content: {type: String},
|
content: {type: String},
|
||||||
|
throwingSetter: {type: String, default: null},
|
||||||
}, {forceId: true});
|
}, {forceId: true});
|
||||||
|
Post.setter.throwingSetter = throwingSetter;
|
||||||
db.automigrate('Post', done);
|
db.automigrate('Post', done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1552,6 +1567,13 @@ describe('manipulation', function() {
|
||||||
done();
|
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() {
|
describe('findOrCreate', function() {
|
||||||
|
|
Loading…
Reference in New Issue