Merge pull request #1433 from strongloop/create-update-pass
Catch errors using cb
This commit is contained in:
commit
9096e0873e
24
lib/dao.js
24
lib/dao.js
|
@ -283,15 +283,19 @@ DataAccessObject.create = function(data, options, cb) {
|
||||||
var obj;
|
var obj;
|
||||||
var idValue = getIdValue(this, data);
|
var idValue = getIdValue(this, data);
|
||||||
|
|
||||||
|
try {
|
||||||
// if we come from save
|
// if we come from save
|
||||||
if (data instanceof Model && !idValue) {
|
if (data instanceof Model && !idValue) {
|
||||||
obj = data;
|
obj = data;
|
||||||
} else {
|
} else {
|
||||||
obj = new Model(data);
|
obj = new Model(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.applyProperties(enforced, obj);
|
this.applyProperties(enforced, obj);
|
||||||
obj.setAttributes(enforced);
|
obj.setAttributes(enforced);
|
||||||
|
} catch (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
|
||||||
Model = this.lookupModel(data); // data-specific
|
Model = this.lookupModel(data); // data-specific
|
||||||
if (Model !== obj.constructor) obj = new Model(data);
|
if (Model !== obj.constructor) obj = new Model(data);
|
||||||
|
@ -3142,7 +3146,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) {
|
||||||
|
|
|
@ -32,11 +32,10 @@ describe('datatypes', function() {
|
||||||
list: {type: ['object']},
|
list: {type: ['object']},
|
||||||
});
|
});
|
||||||
|
|
||||||
(function() {
|
myModel.create({list: 'This string will crash the server'}, function(err) {
|
||||||
myModel.create({list: 'This string will crash the server'});
|
(err.statusCode).should.equal(400);
|
||||||
}).should.throw({statusCode: 400});
|
done();
|
||||||
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 400 when property of type array is set to object value',
|
it('should return 400 when property of type array is set to object value',
|
||||||
|
@ -45,11 +44,10 @@ describe('datatypes', function() {
|
||||||
list: {type: ['object']},
|
list: {type: ['object']},
|
||||||
});
|
});
|
||||||
|
|
||||||
(function() {
|
myModel.create({list: {key: 'This string will crash the server'}}, function(err) {
|
||||||
myModel.create({list: {key: 'This string will crash the server'}});
|
(err.statusCode).should.equal(400);
|
||||||
}).should.throw({statusCode: 400});
|
done();
|
||||||
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws an error when property of type Date is set to an invalid value',
|
it('throws an error when property of type Date is set to an invalid value',
|
||||||
|
@ -58,9 +56,9 @@ describe('datatypes', function() {
|
||||||
date: {type: Date},
|
date: {type: Date},
|
||||||
});
|
});
|
||||||
|
|
||||||
(function() {
|
myModel.create({date: 'invalid'}, function(err) {
|
||||||
myModel.create({date: 'invalid'});
|
(err.message).should.equal('Invalid date: invalid');
|
||||||
}).should.throw({message: 'Invalid date: invalid'});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should keep types when get read data from db', function(done) {
|
it('should keep types when get read data from db', function(done) {
|
||||||
|
|
|
@ -36,6 +36,7 @@ describe('manipulation', function() {
|
||||||
before(function setupStubUserModel(done) {
|
before(function setupStubUserModel(done) {
|
||||||
StubUser = db.createModel('StubUser', {password: String}, {forceId: true});
|
StubUser = db.createModel('StubUser', {password: String}, {forceId: true});
|
||||||
StubUser.setter.password = function(plain) {
|
StubUser.setter.password = function(plain) {
|
||||||
|
if (plain.length === 0) throw new Error('password cannot be empty');
|
||||||
var hashed = false;
|
var hashed = false;
|
||||||
if (!plain) return;
|
if (!plain) return;
|
||||||
var pos = plain.indexOf('-');
|
var pos = plain.indexOf('-');
|
||||||
|
@ -484,6 +485,23 @@ describe('manipulation', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should reject created StubUser with empty password', function(done) {
|
||||||
|
StubUser.create({email: 'b@example.com', password: ''}, function(err, createdUser) {
|
||||||
|
(err.message).should.match(/password cannot be empty/);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reject updated empty password with updateAttribute', function(done) {
|
||||||
|
StubUser.create({email: 'b@example.com', password: 'abc123'}, function(err, createdUser) {
|
||||||
|
if (err) return done(err);
|
||||||
|
createdUser.updateAttribute('password', '', function(err, updatedUser) {
|
||||||
|
(err.message).should.match(/password cannot be empty/);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should update one attribute', function(done) {
|
it('should update one attribute', function(done) {
|
||||||
person.updateAttribute('name', 'Paul Graham', function(err, p) {
|
person.updateAttribute('name', 'Paul Graham', function(err, p) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
|
|
Loading…
Reference in New Issue