Skip to content

Commit

Permalink
chore: Improve error message for Read-only transaction with bounded s…
Browse files Browse the repository at this point in the history
…taleness
  • Loading branch information
surbhigarg92 committed Dec 24, 2024
1 parent 02c4c1f commit 9f4e1e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,19 @@ class Database extends common.GrpcServiceObject {
? (optionsOrCallback as TimestampBounds)
: {};

if ('maxStaleness' in options || 'minReadTimestamp' in options) {
const error = Object.assign(
new Error(
'maxStaleness / minReadTimestamp is not supported for multi-use read-only transactions.'
),
{
code: 3, // invalid argument
}
) as ServiceError;
callback!(error);
return;
}

return startTrace('Database.getSnapshot', this._traceConfig, span => {
this.pool_.getSession((err, session) => {
if (err) {
Expand Down
24 changes: 24 additions & 0 deletions test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,30 @@ describe('Database', () => {
assert.strictEqual(bounds, fakeTimestampBounds);
});

it('should throw error if maxStaleness is passed in the timestamp bounds to the snapshot', () => {
const fakeTimestampBounds = {maxStaleness: 10};

database.getSnapshot(fakeTimestampBounds, err => {
assert.strictEqual(err.code, 3);
assert.strictEqual(
err.message,
'maxStaleness / minReadTimestamp is not supported for multi-use read-only transactions.'
);
});
});

it('should throw error if minReadTimestamp is passed in the timestamp bounds to the snapshot', () => {
const fakeTimestampBounds = {minReadTimestamp: 10};

database.getSnapshot(fakeTimestampBounds, err => {
assert.strictEqual(err.code, 3);
assert.strictEqual(
err.message,
'maxStaleness / minReadTimestamp is not supported for multi-use read-only transactions.'
);
});
});

it('should begin a snapshot', () => {
beginSnapshotStub.callsFake(() => {});

Expand Down

0 comments on commit 9f4e1e8

Please sign in to comment.