Merge pull request #1266 from strongloop/catch-error-on-updateAttribute

dao: catch sync errors on setAttributes
This commit is contained in:
Kevin Delisle 2017-03-16 18:38:10 -04:00 committed by GitHub
commit edc1fd3d83
3 changed files with 29 additions and 2 deletions

View File

@ -1978,7 +1978,12 @@ DataAccessObject.find = function find(query, options, cb) {
applySetters: false,
persisted: true,
};
var obj = new Model(data, ctorOpts);
var obj;
try {
obj = new Model(data, ctorOpts);
} catch (err) {
return callback(err);
}
if (query && query.include) {
if (query.collect) {
@ -3173,7 +3178,11 @@ function(data, options, cb) {
}
// update instance's properties
inst.setAttributes(data);
try {
inst.setAttributes(data);
} catch (err) {
return cb(err);
}
if (doValidate) {
inst.isValid(function(valid) {

View File

@ -654,6 +654,15 @@ describe('basic-querying', function() {
done();
});
});
it('should fail when querying with an invalid value for a type',
function(done) {
User.find({where: {birthday: 'notadate'}}, function(err, users) {
should.exist(err);
err.message.should.equal('Invalid date: notadate');
done();
});
});
});
});

View File

@ -685,6 +685,15 @@ describe('manipulation', function() {
});
});
it('should fail if field validation fails', function(done) {
person.updateAttributes({'name': 'John', dob: 'notadate'},
function(err, p) {
should.exist(err);
err.message.should.equal('Invalid date: notadate');
done();
});
});
it('should allow model instance on updateAttributes', function(done) {
person.updateAttributes(new Person({'name': 'John', age: undefined}),
function(err, p) {