dao: catch sync errors on setAttributes

Wrap inst.setAttributes in try-catch to prevent synchronous errors
from crashing the application.
This commit is contained in:
Kevin Delisle 2017-03-15 19:28:47 -04:00
parent 1808bd3703
commit a6c5802940
2 changed files with 20 additions and 2 deletions

View File

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

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) { it('should allow model instance on updateAttributes', function(done) {
person.updateAttributes(new Person({'name': 'John', age: undefined}), person.updateAttributes(new Person({'name': 'John', age: undefined}),
function(err, p) { function(err, p) {