From 9f4e1e852ede46b7c9cda8d6a8c72d6fb302d2ef Mon Sep 17 00:00:00 2001 From: surbhigarg92 Date: Tue, 24 Dec 2024 16:19:48 +0530 Subject: [PATCH] chore: Improve error message for Read-only transaction with bounded staleness --- src/database.ts | 13 +++++++++++++ test/database.ts | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/database.ts b/src/database.ts index a81c5cfd5..5b45ba51d 100644 --- a/src/database.ts +++ b/src/database.ts @@ -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) { diff --git a/test/database.ts b/test/database.ts index d12eb3a13..9e3b6db7c 100644 --- a/test/database.ts +++ b/test/database.ts @@ -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(() => {});