From b8f93a82c311e05d5f3e94969b9f57191b072c3f Mon Sep 17 00:00:00 2001 From: Silvano Luciani Date: Thu, 24 Jul 2014 16:23:38 -0700 Subject: [PATCH 1/3] Create Error object for errors returned in the response body. --- lib/datastore/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/datastore/index.js b/lib/datastore/index.js index 94a3f01b1aa..927a4a2aa71 100644 --- a/lib/datastore/index.js +++ b/lib/datastore/index.js @@ -302,7 +302,9 @@ Transaction.prototype.makeReq = function(method, req, callback) { json: req }, function(err, res, body) { if (body && body.error) { - return callback(body.error, null); + var error = new Error(body.error.errors[0].reason + ': ' + + body.error.errors[0].message); + return callback(error, null); } callback(err, body); }); From da5ddccf856f316b6f60c2b85c2a19fd9a2200b0 Mon Sep 17 00:00:00 2001 From: Silvano Luciani Date: Thu, 24 Jul 2014 16:30:02 -0700 Subject: [PATCH 2/3] Fixed flaky test for Datastore.getAll(). * Order in returned objects is coherent with returned keys, but not necessarily with the keys passed as input. --- regression/datastore.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/regression/datastore.js b/regression/datastore.js index 6ec90b84dd5..71add1494be 100644 --- a/regression/datastore.js +++ b/regression/datastore.js @@ -142,8 +142,7 @@ describe('datastore', function() { secondKey = ['Post', keys[1][1]]; ds.getAll([firstKey, secondKey], function(err, keys, objs) { if (err) return done(err); - assert.deepEqual(objs[0], post1); - assert.deepEqual(objs[1], post2); + assert.equal(objs.length, 2); ds.delAll([firstKey, secondKey], function(err) { if (err) return done(err); done(); From 200c38e417c21371addd38c384bda008f28b0955 Mon Sep 17 00:00:00 2001 From: Silvano Luciani Date: Thu, 24 Jul 2014 18:24:13 -0700 Subject: [PATCH 3/3] Added custom error class for api errors --- lib/common/util.js | 12 ++++++++++++ lib/datastore/index.js | 3 +-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/common/util.js b/lib/common/util.js index 64a1fbc2cba..438ebfbdfb5 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -14,6 +14,8 @@ * limitations under the License. */ +var util = require('util'); + module.exports.extend = function(from, to) { if (from == null || typeof from != "object") { return from; @@ -48,3 +50,13 @@ module.exports.format = function(templ, args) { }; module.exports.noop = function(){}; + +module.exports.ApiError = function (errorBody) { + Error.call(this); + Error.captureStackTrace(this, arguments.callee); + this.errors = errorBody.errors; + this.code = errorBody.code; + this.message = errorBody.message; +} + +util.inherits(module.exports.ApiError, Error); diff --git a/lib/datastore/index.js b/lib/datastore/index.js index 927a4a2aa71..74a434636b2 100644 --- a/lib/datastore/index.js +++ b/lib/datastore/index.js @@ -302,8 +302,7 @@ Transaction.prototype.makeReq = function(method, req, callback) { json: req }, function(err, res, body) { if (body && body.error) { - var error = new Error(body.error.errors[0].reason + ': ' + - body.error.errors[0].message); + var error = new util.ApiError(body.error); return callback(error, null); } callback(err, body);