Skip to content

Commit

Permalink
Merge pull request #153 from proppy/add-error-handling
Browse files Browse the repository at this point in the history
datastore/request: add API error handling
  • Loading branch information
Burcu Dogan committed Aug 30, 2014
2 parents bac5d90 + f06a4b1 commit d539bf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ function handleResp(err, resp, body, callback) {
return;
}
if (resp && (resp.statusCode < 200 || resp.statusCode > 299)) {
callback(new Error('error during request, statusCode: ' + resp.statusCode));
var error = 'error during request, statusCode: ' + resp.statusCode;
if (body) {
error += ', body: ' + body;
}
callback(new Error(error));
return;
}
callback(null, body, resp);
Expand Down
8 changes: 7 additions & 1 deletion lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,13 @@ Transaction.prototype.makeReq = function(method, req, respType, callback) {
buffer = Buffer.concat([buffer, chunk]);
});
resp.on('end', function() {
callback(null, respType.decode(buffer));
util.handleResp(null, resp, buffer.toString(), function(err) {
if (err) {
callback(err);
return;
}
callback(null, respType.decode(buffer));
});
});
});
remoteStream.on('error', callback);
Expand Down

0 comments on commit d539bf0

Please sign in to comment.