From b1b2d5df0c2c00069b8feaceef7f17495ac4e675 Mon Sep 17 00:00:00 2001 From: loay Date: Mon, 24 Jul 2017 00:58:35 -0400 Subject: [PATCH] Catch errors using cb --- lib/dao.js | 18 +++++++++++------- test/datatype.test.js | 18 ++++++++---------- test/manipulation.test.js | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 60fc81a9..d314a288 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -312,15 +312,19 @@ DataAccessObject.create = function(data, options, cb) { var obj; var idValue = getIdValue(this, data); + try { // if we come from save - if (data instanceof Model && !idValue) { - obj = data; - } else { - obj = new Model(data); - } + if (data instanceof Model && !idValue) { + obj = data; + } else { + obj = new Model(data); + } - this.applyProperties(enforced, obj); - obj.setAttributes(enforced); + this.applyProperties(enforced, obj); + obj.setAttributes(enforced); + } catch (err) { + return cb(err); + } Model = this.lookupModel(data); // data-specific if (Model !== obj.constructor) obj = new Model(data); diff --git a/test/datatype.test.js b/test/datatype.test.js index 84c013b8..c4c9c0b8 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -34,11 +34,10 @@ describe('datatypes', function() { list: {type: ['object']}, }); - (function() { - myModel.create({list: 'This string will crash the server'}); - }).should.throw({statusCode: 400}); - - done(); + myModel.create({list: 'This string will crash the server'}, function(err) { + (err.statusCode).should.equal(400); + done(); + }); }); it('should return 400 when property of type array is set to object value', @@ -47,11 +46,10 @@ describe('datatypes', function() { list: {type: ['object']}, }); - (function() { - myModel.create({list: {key: 'This string will crash the server'}}); - }).should.throw({statusCode: 400}); - - done(); + myModel.create({list: {key: 'This string will crash the server'}}, function(err) { + (err.statusCode).should.equal(400); + done(); + }); }); it('should keep types when get read data from db', function(done) { diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 08878130..b6673115 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -42,6 +42,7 @@ describe('manipulation', function() { before(function setupStubUserModel(done) { StubUser = db.createModel('StubUser', {password: String}, {forceId: true}); StubUser.setter.password = function(plain) { + if (plain.length === 0) throw new Error('password cannot be empty'); var hashed = false; if (!plain) return; var pos = plain.indexOf('-'); @@ -542,6 +543,22 @@ 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({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) { person.updateAttribute('name', 'Paul Graham', function(err, p) { if (err) return done(err);