Skip to content

Commit

Permalink
Handle invalid json response
Browse files Browse the repository at this point in the history
When receiving an invalid json string, catch the json
error generated and transmit to callback.
  • Loading branch information
Rémi Parpaillon committed May 24, 2018
1 parent 4c0d490 commit 8765954
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,20 @@ module.exports = function (classes){

response.on('end', function responseEnd(){
if (response.statusCode !== 200) {
callback(new Error('"' + response.statusCode + '"' + data))
;
callback(new Error('"' + response.statusCode + '"' + data));
return;
}
var decoded = JSON.parse(data);

var decoded;
try {
decoded = JSON.parse(data);
} catch (error) {
decoded = {
error: error,
result: null
};
}

if (_.isFunction(callback)) {
if (!decoded.error) {
decoded.error = null;
Expand Down

0 comments on commit 8765954

Please sign in to comment.