diff --git a/src/Collection.js b/src/Collection.js index 4e1c16735..befb51b61 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -367,7 +367,7 @@ Collection.prototype.mCreateDocument = function (documents, options, cb) { }); self.kuzzle.query(this.buildQueryArgs('document', 'mCreate'), data, options, cb && function (err, res) { - cb(err, err ? undefined : res.result); + cb(err, res && res.result); }); return self; @@ -403,7 +403,7 @@ Collection.prototype.mCreateOrReplaceDocument = function (documents, options, cb }); self.kuzzle.query(this.buildQueryArgs('document', 'mCreateOrReplace'), data, options, cb && function (err, res) { - cb(err, err ? undefined : res.result); + cb(err, res && res.result); }); return self; @@ -437,7 +437,7 @@ Collection.prototype.mDeleteDocument = function (documentIds, options, cb) { self.kuzzle.callbackRequired('Collection.mDelete', cb); self.kuzzle.query(this.buildQueryArgs('document', 'mDelete'), data, options, cb && function (err, res) { - cb(err, err ? undefined : res.result); + cb(err, res && res.result); }); return self; @@ -540,7 +540,7 @@ Collection.prototype.mUpdateDocument = function (documents, options, cb) { }); self.kuzzle.query(this.buildQueryArgs('document', 'mUpdate'), data, options, cb && function (err, res) { - cb(err, err ? undefined : res.result); + cb(err, res && res.result); }); return self; diff --git a/test/Collection/methods.test.js b/test/Collection/methods.test.js index fec2d3afc..1bf950228 100644 --- a/test/Collection/methods.test.js +++ b/test/Collection/methods.test.js @@ -866,6 +866,33 @@ describe('Collection methods', function () { kuzzle.query.yield(null, { result: result }); }); + + it('should handle partial errors', function (done) { + this.timeout(50); + + collection.mCreateDocument(content.documents, function (err, res) { + should(res).be.instanceof(Object); + should(res).match(result); + should(err).be.instanceof(Object); + should(err).match({status: 206}); + done(); + }); + + kuzzle.query.yield({status: 206}, { result: result }); + }); + + it('should handle global errors', function (done) { + this.timeout(50); + + collection.mCreateDocument(content.documents, function (err, res) { + should(res).be.undefined(); + should(err).be.instanceof(Object); + should(err).match({status: 503}); + done(); + }); + + kuzzle.query.yield({status: 503}); + }); }); describe('#mCreateOrReplaceDocument', function () { @@ -916,6 +943,33 @@ describe('Collection methods', function () { kuzzle.query.yield(null, { result: result }); }); + + it('should handle partial errors', function (done) { + this.timeout(50); + + collection.mCreateOrReplaceDocument(content.documents, function (err, res) { + should(res).be.instanceof(Object); + should(res).match(result); + should(err).be.instanceof(Object); + should(err).match({status: 206}); + done(); + }); + + kuzzle.query.yield({status: 206}, { result: result }); + }); + + it('should handle global errors', function (done) { + this.timeout(50); + + collection.mCreateOrReplaceDocument(content.documents, function (err, res) { + should(res).be.undefined(); + should(err).be.instanceof(Object); + should(err).match({status: 503}); + done(); + }); + + kuzzle.query.yield({status: 503}); + }); }); describe('#mDeleteDocument', function () { @@ -960,6 +1014,33 @@ describe('Collection methods', function () { kuzzle.query.yield(null, { result: result }); }); + + it('should handle partial errors', function (done) { + this.timeout(50); + + collection.mDeleteDocument(content.ids, function (err, res) { + should(res).be.instanceof(Object); + should(res).match(result); + should(err).be.instanceof(Object); + should(err).match({status: 206}); + done(); + }); + + kuzzle.query.yield({status: 206}, { result: result }); + }); + + it('should handle global errors', function (done) { + this.timeout(50); + + collection.mDeleteDocument(content.ids, function (err, res) { + should(res).be.undefined(); + should(err).be.instanceof(Object); + should(err).match({status: 503}); + done(); + }); + + kuzzle.query.yield({status: 503}); + }); }); describe('#mGetDocument', function () { @@ -1096,13 +1177,41 @@ describe('Collection methods', function () { this.timeout(50); collection.mUpdateDocument(content.documents, function (err, res) { - should(res).should.be.instanceof(Object); + should(res).be.instanceof(Object); should(res).match(result); + should(err).be.null(); done(); }); kuzzle.query.yield(null, { result: result }); }); + + it('should handle partial errors', function (done) { + this.timeout(50); + + collection.mUpdateDocument(content.documents, function (err, res) { + should(res).be.instanceof(Object); + should(res).match(result); + should(err).be.instanceof(Object); + should(err).match({status: 206}); + done(); + }); + + kuzzle.query.yield({status: 206}, { result: result }); + }); + + it('should handle global errors', function (done) { + this.timeout(50); + + collection.mUpdateDocument(content.documents, function (err, res) { + should(res).be.undefined(); + should(err).be.instanceof(Object); + should(err).match({status: 503}); + done(); + }); + + kuzzle.query.yield({status: 503}); + }); }); describe('#getSpecifications', function () {