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 Nov 10, 2017
1 parent 4c0d490 commit ffd87a0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,18 @@ 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) {
callback(error);
return;
}

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

0 comments on commit ffd87a0

Please sign in to comment.