From 3e759bd64962afb56d345236fa7700346d6d5f9b Mon Sep 17 00:00:00 2001
From: Johan Euphrosine
Date: Fri, 29 Aug 2014 15:56:44 -0700
Subject: [PATCH 1/5] datastore/request: add API error handling
---
lib/datastore/transaction.js | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/lib/datastore/transaction.js b/lib/datastore/transaction.js
index ee2f9ac49f17..02e84bc4017d 100644
--- a/lib/datastore/transaction.js
+++ b/lib/datastore/transaction.js
@@ -446,7 +446,15 @@ Transaction.prototype.makeReq = function(method, req, respType, callback) {
buffer = Buffer.concat([buffer, chunk]);
});
resp.on('end', function() {
- callback(null, respType.decode(buffer));
+ if (resp.statusCode != 200) {
+ return callback({
+ error: 'request failed',
+ request: request,
+ response: resp,
+ body: buffer.toString()
+ }, null);
+ }
+ return callback(null, respType.decode(buffer));
});
});
remoteStream.on('error', callback);
From 53d0599c5c4960de10f07a21d88620c8ae101c28 Mon Sep 17 00:00:00 2001
From: Johan Euphrosine
Date: Fri, 29 Aug 2014 16:42:15 -0700
Subject: [PATCH 2/5] datastore/request: handle API response with
util.handleResp
---
lib/common/util.js | 2 +-
lib/datastore/transaction.js | 16 +++++++---------
2 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/lib/common/util.js b/lib/common/util.js
index e4f5384cf0c5..226bfe3fc66d 100644
--- a/lib/common/util.js
+++ b/lib/common/util.js
@@ -149,7 +149,7 @@ function handleResp(err, resp, body, callback) {
return;
}
if (resp && (resp.statusCode < 200 || resp.statusCode > 299)) {
- callback(new Error('error during request, statusCode: ' + resp.statusCode));
+ callback(new Error('error during request, statusCode: ' + resp.statusCode + ', body: ' + body));
return;
}
callback(null, body, resp);
diff --git a/lib/datastore/transaction.js b/lib/datastore/transaction.js
index 02e84bc4017d..5891113e0984 100644
--- a/lib/datastore/transaction.js
+++ b/lib/datastore/transaction.js
@@ -446,15 +446,13 @@ Transaction.prototype.makeReq = function(method, req, respType, callback) {
buffer = Buffer.concat([buffer, chunk]);
});
resp.on('end', function() {
- if (resp.statusCode != 200) {
- return callback({
- error: 'request failed',
- request: request,
- response: resp,
- body: buffer.toString()
- }, null);
- }
- return callback(null, respType.decode(buffer));
+ util.handleResp(null, resp, buffer.toString(), function(err) {
+ if (err) {
+ callback(err);
+ return;
+ }
+ return callback(null, respType.decode(buffer));
+ });
});
});
remoteStream.on('error', callback);
From ba49f39a08eef62335cc35cc38cd7d3b0a499b82 Mon Sep 17 00:00:00 2001
From: Johan Euphrosine
Date: Fri, 29 Aug 2014 16:43:19 -0700
Subject: [PATCH 3/5] common/util: lint
---
lib/common/util.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/lib/common/util.js b/lib/common/util.js
index 226bfe3fc66d..9e5948418bad 100644
--- a/lib/common/util.js
+++ b/lib/common/util.js
@@ -149,7 +149,8 @@ function handleResp(err, resp, body, callback) {
return;
}
if (resp && (resp.statusCode < 200 || resp.statusCode > 299)) {
- callback(new Error('error during request, statusCode: ' + resp.statusCode + ', body: ' + body));
+ callback(new Error('error during request, statusCode: ' + resp.statusCode +
+ ', body: ' + body));
return;
}
callback(null, body, resp);
From 7537fd57111aab3c5ab432ff25fa08dd5c8040f9 Mon Sep 17 00:00:00 2001
From: Johan Euphrosine
Date: Fri, 29 Aug 2014 17:22:31 -0700
Subject: [PATCH 4/5] datastore/request: remove useless return
---
lib/datastore/transaction.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/datastore/transaction.js b/lib/datastore/transaction.js
index 5891113e0984..2738a92f8aa8 100644
--- a/lib/datastore/transaction.js
+++ b/lib/datastore/transaction.js
@@ -451,7 +451,7 @@ Transaction.prototype.makeReq = function(method, req, respType, callback) {
callback(err);
return;
}
- return callback(null, respType.decode(buffer));
+ callback(null, respType.decode(buffer));
});
});
});
From f06a4b15fa9a65b8ec56096c0686ebac120a4533 Mon Sep 17 00:00:00 2001
From: Johan Euphrosine
Date: Fri, 29 Aug 2014 17:28:45 -0700
Subject: [PATCH 5/5] common/util/handleResp: only add body when non null
---
lib/common/util.js | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/lib/common/util.js b/lib/common/util.js
index 9e5948418bad..bb4e80e26ab4 100644
--- a/lib/common/util.js
+++ b/lib/common/util.js
@@ -149,8 +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 +
- ', body: ' + body));
+ var error = 'error during request, statusCode: ' + resp.statusCode;
+ if (body) {
+ error += ', body: ' + body;
+ }
+ callback(new Error(error));
return;
}
callback(null, body, resp);