Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Begin transaction does not handle error #1833

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transaction.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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