Skip to content

Commit

Permalink
swallow error for automatic rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jun 22, 2016
1 parent 05145f3 commit 7ccc1b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
17 changes: 4 additions & 13 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,10 @@ Transaction.prototype.commit = function(callback) {
this.request_(protoOpts, reqOpts, function(err, resp) {
if (err) {
// Rollback automatically for the user.
self.rollback(function(err) {
if (err) {
err.message = [
'The commit was not successful and the transaction could not be',
'rolled back. You may want to try calling transaction.rollback().',
'\n',
'\n',
err.message
].join('');
}

// Provide the response items from the failed commit to the user. A
// successful rollback should be transparent.
self.rollback(function() {
// Provide the response items from the failed commit to the user. Even
// a failed rollback should be transparent.
// RE: https://github.com/GoogleCloudPlatform/gcloud-node/pull/1369#discussion_r66833976
callback(err, resp);
});
return;
Expand Down
28 changes: 20 additions & 8 deletions test/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,27 @@ describe('Transaction', function() {
transaction.commit(done);
});

it('should pass error to callback', function(done) {
describe('errors', function() {
var error = new Error('Error.');
transaction.request_ = function(protoOpts, reqOpts, callback) {
callback = callback || reqOpts;
callback(error);
};
transaction.commit(function(err) {
assert.deepEqual(err, error);
done();
var apiResponse = {};

beforeEach(function() {
transaction.rollback = function(callback) {
callback();
};

transaction.request_ = function(protoOpts, reqOpts, callback) {
callback = callback || reqOpts;
callback(error, apiResponse);
};
});

it('should pass the error to the callback', function(done) {
transaction.commit(function(err, resp) {
assert.strictEqual(err, error);
assert.strictEqual(resp, apiResponse);
done();
});
});
});

Expand Down

0 comments on commit 7ccc1b2

Please sign in to comment.