Skip to content

Commit

Permalink
fix: Begin transaction foes not handle error (#1833)
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Apr 6, 2023
1 parent cbdc3e8 commit 6ecd366
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/transaction.ts
Expand Up @@ -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;
}

Expand Down
24 changes: 24 additions & 0 deletions test/spanner.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 6ecd366

Please sign in to comment.