Skip to content

Commit 7cb353e

Browse files
fix: missing await in rollback (#671)
1 parent 74e157c commit 7cb353e

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

samples/concepts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,15 +1102,15 @@ class Transaction extends TestHelper {
11021102
const [task] = await transaction.get(taskKey);
11031103
if (task) {
11041104
// The task entity already exists.
1105-
transaction.rollback();
1105+
await transaction.rollback();
11061106
} else {
11071107
// Create the task entity.
11081108
transaction.save(taskEntity);
11091109
transaction.commit();
11101110
}
11111111
return taskEntity;
11121112
} catch (err) {
1113-
transaction.rollback();
1113+
await transaction.rollback();
11141114
}
11151115
}
11161116
// [END datastore_transactional_get_or_create]

samples/tasks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async function markDone(taskId) {
112112
await transaction.commit();
113113
console.log(`Task ${taskId} updated successfully.`);
114114
} catch (err) {
115-
transaction.rollback();
115+
await transaction.rollback();
116116
}
117117
}
118118
// [END datastore_update_entity]

samples/tasks.markdone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function main(taskId) {
4444
await transaction.commit();
4545
console.log(`Task ${taskId} updated successfully.`);
4646
} catch (err) {
47-
transaction.rollback();
47+
await transaction.rollback();
4848
throw err;
4949
}
5050
}

src/request.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,13 @@ class DatastoreRequest {
13011301
const transaction = this.datastore.transaction();
13021302
transaction.run(async err => {
13031303
if (err) {
1304-
transaction.rollback();
1304+
try {
1305+
await transaction.rollback();
1306+
} catch (error) {
1307+
// Provide the error & API response from the failed run to the user.
1308+
// Even a failed rollback should be transparent.
1309+
// RE: https://github.com/GoogleCloudPlatform/gcloud-node/pull/1369#discussion_r66833976
1310+
}
13051311
callback!(err);
13061312
return;
13071313
}
@@ -1321,7 +1327,13 @@ class DatastoreRequest {
13211327
const [response] = await transaction.commit();
13221328
callback!(null, response);
13231329
} catch (err) {
1324-
transaction.rollback();
1330+
try {
1331+
await transaction.rollback();
1332+
} catch (error) {
1333+
// Provide the error & API response from the failed commit to the user.
1334+
// Even a failed rollback should be transparent.
1335+
// RE: https://github.com/GoogleCloudPlatform/gcloud-node/pull/1369#discussion_r66833976
1336+
}
13251337
callback!(err);
13261338
}
13271339
});

test/request.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,38 @@ describe('Request', () => {
22512251
done();
22522252
});
22532253
});
2254+
2255+
it('should avoid the rollback exception in transaction.run', done => {
2256+
sandbox
2257+
.stub(transaction, 'run')
2258+
.callsFake((gaxOption, callback?: Function) => {
2259+
callback = typeof gaxOption === 'function' ? gaxOption : callback!;
2260+
callback(new Error('Error.'));
2261+
});
2262+
2263+
sandbox
2264+
.stub(transaction, 'rollback')
2265+
.rejects(new Error('Rollback Error.'));
2266+
2267+
request.merge({key, data: null}, (err: Error) => {
2268+
assert.strictEqual(err.message, 'Error.');
2269+
done();
2270+
});
2271+
});
2272+
2273+
it('should avoid the rollback exception in transaction.get/commit', done => {
2274+
sandbox.restore();
2275+
sandbox.stub(transaction, 'get').rejects(new Error('Error.'));
2276+
2277+
sandbox
2278+
.stub(transaction, 'rollback')
2279+
.rejects(new Error('Rollback Error.'));
2280+
2281+
request.merge({key, data: null}, (err: Error) => {
2282+
assert.strictEqual(err.message, 'Error.');
2283+
done();
2284+
});
2285+
});
22542286
});
22552287

22562288
describe('request_', () => {

0 commit comments

Comments
 (0)