Skip to content

Commit

Permalink
ELE-4109: Uncaught Babel client error when Babel 502s (#27)
Browse files Browse the repository at this point in the history
* ELE-4109: Uncaught error thrown in client when Babel 503s

Babel client assumes we will always get a response body

* Linting & typo broken windows

* Add tests

* Bump version number
  • Loading branch information
Camille Fenton authored Feb 10, 2021
1 parent b8b5b50 commit 170e327
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 33 deletions.
44 changes: 20 additions & 24 deletions babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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));
};
Expand All @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -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));
};
Expand Down
88 changes: 80 additions & 8 deletions babel/test/unit/babel_client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -313,7 +313,7 @@ describe("Babel Node Client Test Suite", function(){
annotations
}
})
})
})

var requestStubSpy = sinon.spy(requestStub);
babel.__set__("rpn", requestStubSpy);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "talis-node",
"version": "0.1.6",
"version": "0.1.7",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 170e327

Please sign in to comment.