From 6ecd366da7183d502c710cb5c879984c276b12db Mon Sep 17 00:00:00 2001 From: surbhigarg92 Date: Thu, 6 Apr 2023 08:11:52 +0000 Subject: [PATCH] fix: Begin transaction foes not handle error (#1833) --- src/transaction.ts | 2 +- test/spanner.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/transaction.ts b/src/transaction.ts index 853bfb77f..2d771b38c 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -1774,7 +1774,7 @@ export class Transaction extends Dml { } else if (!this._useInRunner) { reqOpts.singleUseTransaction = this._options; } else { - this.begin().then(() => this.commit(options, callback)); + this.begin().then(() => this.commit(options, callback), callback); return; } diff --git a/test/spanner.ts b/test/spanner.ts index bd399e204..8b24a2468 100644 --- a/test/spanner.ts +++ b/test/spanner.ts @@ -3382,6 +3382,30 @@ describe('Spanner with mock server', () => { }) as v1.BeginTransactionRequest; assert.ok(beginTxnRequest, 'beginTransaction was called'); }); + + it('should throw error if begin transaction fails on blind commit', async () => { + const database = newTestDatabase(); + const err = { + message: 'Test error', + } as MockError; + spannerMock.setExecutionTime( + spannerMock.beginTransaction, + SimulatedExecutionTime.ofError(err) + ); + try { + await database.runTransactionAsync(async tx => { + tx.insert('foo', {id: 1, name: 'One'}); + await tx.commit(); + }); + } catch (e) { + assert.strictEqual( + (e as ServiceError).message, + '2 UNKNOWN: Test error' + ); + } finally { + await database.close(); + } + }); }); describe('table', () => {