Skip to content

Commit

Permalink
Merge pull request #34 from pcdevil/master
Browse files Browse the repository at this point in the history
Add missing tests and fix json response handling
  • Loading branch information
sonicoder86 authored Nov 24, 2017
2 parents bbd5ee5 + 01ad8d1 commit f1c2edd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class RequestWrapper {
throw new SuiteRequestError(
'Error in http response (status: ' + response.statusCode + ')',
response.statusCode,
response.body
this._parseBody(response)
);
}

Expand All @@ -105,13 +105,19 @@ class RequestWrapper {
throw new SuiteRequestError('Empty http response', 500, response.statusMessage);
}

if (this._isJsonResponse(response)) {
try {
response.body = JSON.parse(response.body);
} catch (ex) {
logger.error('fatal_error', ex, this._getLogParameters());
throw new SuiteRequestError(ex.message, 500);
}
response.body = this._parseBody(response);
}

_parseBody(response) {
if (!this._isJsonResponse(response)) {
return response.body;
}

try {
return JSON.parse(response.body);
} catch (ex) {
logger.error('fatal_error', ex, this._getLogParameters());
throw new SuiteRequestError(ex.message, 500);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions lib/wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('Wrapper', function() {
expect(err).to.be.an.instanceof(SuiteRequestError);
expect(err.message).to.eql('Error in http response (status: 400)');
expect(err.code).to.eql(400);
expect(err.data).to.eql({ replyText: 'Unknown route' });
expect(requestGetStub).to.be.calledWith(expectedRequestOptions);
}
});
Expand Down Expand Up @@ -135,6 +136,22 @@ describe('Wrapper', function() {
}
throw new Error('Error should have been thrown');
});

it('should throw a http response error even if the response body is empty', function *() {
apiResponse.body = '404 Not Found';
apiResponse.statusCode = 404;
apiResponse.headers = { 'content-type': 'text/plain' };

try {
yield wrapper.send();
throw new Error('should throw');
} catch (err) {
expect(err).to.be.an.instanceOf(SuiteRequestError);
expect(err.code).to.eql(apiResponse.statusCode);
expect(err.message).to.eql('Error in http response (status: 404)');
expect(err.data).to.eql(apiResponse.body);
}
});
});


Expand Down

0 comments on commit f1c2edd

Please sign in to comment.