diff --git a/babel/index.js b/babel/index.js index cafe93b..964beeb 100644 --- a/babel/index.js +++ b/babel/index.js @@ -139,17 +139,17 @@ BabelClient.prototype.getEntireTargetFeed = async function (target, token, hydra this.debug(JSON.stringify(requestOptions)); try { - const { + const { body: { feed_length, annotations, userProfiles, error, error_description, - }, + }, ...response } = await rpn(requestOptions); - + if (error) { callbackError = new Error(error_description); callbackError.http_code = response.statusCode || 404; @@ -267,7 +267,7 @@ BabelClient.prototype.getFeeds = function getFeeds(feeds, token, callback) { this.debug(JSON.stringify(requestOptions)); - request(requestOptions, function requesResponse(err, response, body) { + request(requestOptions, function requestResponse(err, response, body) { if (err) { callback(err); } else { @@ -364,7 +364,7 @@ BabelClient.prototype.getAnnotations = function getAnnotations(token, querystrin * * @param {string} token Persona token * @param {object} data Data that can be passed into an annotation - * @param {object} data.hasbody + * @param {object} data.hasBody * @param {string} data.hasBody.format * @param {string} data.hasBody.type * @param {string} data.hasBody.chars @@ -376,7 +376,7 @@ BabelClient.prototype.getAnnotations = function getAnnotations(token, querystrin * @param {string} data.hasTarget.fragment * @param {string} data.hasTarget.asReferencedBy * @param {string} data.annotatedBy - * @param {string} data.motiviatedBy + * @param {string} data.motivatedBy * @param {string} data.annotatedAt * @param {object} options that control the request being made to babel. * @param {boolean} options.headers['X-Ingest-Synchronously'] @@ -453,16 +453,14 @@ BabelClient.prototype.createAnnotation = function createAnnotation(token, data, this.debug(JSON.stringify(requestOptions)); request.post(requestOptions, function requestResponse(err, response, body){ - if(err){ + if (err) { callback(err); - } else{ - if(!this._responseSuccessful(response) || (body.message && body.errors)){ - var babelError = new Error(body.message); - babelError.http_code = response.statusCode || 404; - callback(babelError); - } else{ - callback(null, body); - } + } else if (!this._responseSuccessful(response)) { + var babelError = new Error('Error creating annotation: ' + JSON.stringify(body)); + babelError.http_code = response && response.statusCode ? response.statusCode : 404; + callback(babelError); + } else { + callback(null, body); } }.bind(this)); }; @@ -473,7 +471,7 @@ BabelClient.prototype.createAnnotation = function createAnnotation(token, data, * @param {string} token Persona token * @param {object} data Data that can be passed into an annotation * @param {object} data._id - * @param {object} data.hasbody + * @param {object} data.hasBody * @param {string} data.hasBody.format * @param {string} data.hasBody.type * @param {string} data.hasBody.chars @@ -485,7 +483,7 @@ BabelClient.prototype.createAnnotation = function createAnnotation(token, data, * @param {string} data.hasTarget.fragment * @param {string} data.hasTarget.asReferencedBy * @param {string} data.annotatedBy - * @param {string} data.motiviatedBy + * @param {string} data.motivatedBy * @param {string} data.annotatedAt * @param callback */ @@ -553,14 +551,12 @@ BabelClient.prototype.updateAnnotation = function updateAnnotation(token, data, request.put(requestOptions, function requestResponse(err, response, body){ if (err) { callback(err); + } else if (!this._responseSuccessful(response)) { + var babelError = new Error('Error updating annotation: ' + JSON.stringify(body)); + babelError.http_code = response && response.statusCode ? response.statusCode : 404; + callback(babelError); } else { - if(!this._responseSuccessful(response) || (body.message && body.errors)){ - var babelError = new Error(body.message); - babelError.http_code = response.statusCode || 404; - callback(babelError); - } else { - callback(null, body); - } + callback(null, body); } }.bind(this)); }; diff --git a/babel/test/unit/babel_client_test.js b/babel/test/unit/babel_client_test.js index 6ddb850..de59d84 100644 --- a/babel/test/unit/babel_client_test.js +++ b/babel/test/unit/babel_client_test.js @@ -206,7 +206,7 @@ describe("Babel Node Client Test Suite", function(){ babel_host:"http://babel", babel_port:3000 }); - var requestStub = () => new Promise ((_resolve, reject) => reject(Error('Error communicating with Babel'))) + var requestStub = () => new Promise ((_resolve, reject) => reject(Error('Error communicating with Babel'))) babel.__set__("rpn", requestStub); @@ -227,10 +227,10 @@ describe("Babel Node Client Test Suite", function(){ var requestStub = () => new Promise ((resolve, reject) => resolve({ statusCode: 401, body: { - error:"invalid_token", + error:"invalid_token", error_description:"The token is invalid or has expired" } - })) + })) babel.__set__("rpn", requestStub); @@ -252,10 +252,10 @@ describe("Babel Node Client Test Suite", function(){ var requestStub = () => new Promise ((resolve, _reject) => resolve({ statusCode: 404, body: { - error:"feed_not_found", + error:"feed_not_found", error_description:"Feed not found" } - })) + })) babel.__set__("rpn", requestStub); @@ -313,7 +313,7 @@ describe("Babel Node Client Test Suite", function(){ annotations } }) - }) + }) var requestStubSpy = sinon.spy(requestStub); babel.__set__("rpn", requestStubSpy); @@ -1155,12 +1155,48 @@ describe("Babel Node Client Test Suite", function(){ babelClient.createAnnotation('secret', {hasBody:{format:'text/plain', type:'Text'}, hasTarget:{uri:'http://example.com'}, annotatedBy:'Gordon Freeman'}, {}, function(err, result){ (err === null).should.be.false; - err.message.should.equal('Bad Request'); + err.message.should.equal( + 'Error creating annotation: {"body":"","message":"Bad Request"}' + ); (typeof result).should.equal('undefined'); done(); }); }); + it("- should return an error if call to request returns a 502 response with no body", function (done) { + var babel = rewire("../../index.js"); + + var babelClient = babel.createClient({ + babel_host: "http://babel", + babel_port: 3000, + }); + var requestStub = { + post: function (options, callback) { + callback(null, { statusCode: 502 }, { message: "Bad Gateway" }); + }, + }; + + babel.__set__("request", requestStub); + + babelClient.createAnnotation( + "secret", + { + hasBody: { format: "text/plain", type: "Text" }, + hasTarget: { uri: "http://example.com" }, + annotatedBy: "Gordon Freeman", + }, + {}, + function (err, result) { + (err === null).should.be.false; + err.message.should.equal( + 'Error creating annotation: {"message":"Bad Gateway"}' + ); + (typeof result).should.equal("undefined"); + done(); + } + ); + }); + it("- should return no errors if everything is successful", function(done){ var babel = rewire("../../index.js"); @@ -1512,12 +1548,48 @@ describe("Babel Node Client Test Suite", function(){ babelClient.updateAnnotation('secret', {_id: 'testid', hasBody:{format:'text/plain', type:'Text'}, hasTarget:{uri:'http://example.com'}, annotatedBy:'Gordon Freeman'}, function(err, result){ (err === null).should.be.false; - err.message.should.equal('Bad Request'); + err.message.should.equal( + 'Error updating annotation: {"body":"","message":"Bad Request"}' + ); (typeof result).should.equal('undefined'); done(); }); }); + it("- should return an error if call to request returns a 502 response with no body", function (done) { + var babel = rewire("../../index.js"); + + var babelClient = babel.createClient({ + babel_host: "http://babel", + babel_port: 3000, + }); + var requestStub = { + put: function (options, callback) { + callback(null, { statusCode: 400 }, { message: "Bad Gateway" }); + }, + }; + + babel.__set__("request", requestStub); + + babelClient.updateAnnotation( + "secret", + { + _id: "testid", + hasBody: { format: "text/plain", type: "Text" }, + hasTarget: { uri: "http://example.com" }, + annotatedBy: "Gordon Freeman", + }, + function (err, result) { + (err === null).should.be.false; + err.message.should.equal( + 'Error updating annotation: {"message":"Bad Gateway"}' + ); + (typeof result).should.equal("undefined"); + done(); + } + ); + }); + it("- should return no errors if everything is successful", function(done){ var babel = rewire("../../index.js"); diff --git a/package.json b/package.json index 553e074..40d7e44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "talis-node", - "version": "0.1.6", + "version": "0.1.7", "description": "", "main": "index.js", "scripts": {