Skip to content

Commit 559031d

Browse files
chore: Improve error message for Read-only transaction with bounded staleness (#2207)
* chore: integration test fix * chore: Improve error message for Read-only transaction with bounded staleness * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * review comments * review comments * review comment --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 3cc257e commit 559031d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/database.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ import {
8585
LongRunningCallback,
8686
NormalCallback,
8787
PagedOptionsWithFilter,
88-
CLOUD_RESOURCE_HEADER,
8988
PagedResponse,
9089
RequestCallback,
9190
ResourceCallback,
@@ -2095,6 +2094,26 @@ class Database extends common.GrpcServiceObject {
20952094
? (optionsOrCallback as TimestampBounds)
20962095
: {};
20972096

2097+
if (
2098+
('maxStaleness' in options &&
2099+
options.maxStaleness !== null &&
2100+
options.maxStaleness !== undefined) ||
2101+
('minReadTimestamp' in options &&
2102+
options.minReadTimestamp !== null &&
2103+
options.minReadTimestamp !== undefined)
2104+
) {
2105+
const error = Object.assign(
2106+
new Error(
2107+
'maxStaleness / minReadTimestamp is not supported for multi-use read-only transactions.'
2108+
),
2109+
{
2110+
code: 3, // invalid argument
2111+
}
2112+
) as ServiceError;
2113+
callback!(error);
2114+
return;
2115+
}
2116+
20982117
return startTrace('Database.getSnapshot', this._traceConfig, span => {
20992118
this.pool_.getSession((err, session) => {
21002119
if (err) {

test/database.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,39 @@ describe('Database', () => {
23072307
assert.strictEqual(bounds, fakeTimestampBounds);
23082308
});
23092309

2310+
it('should throw error if maxStaleness is passed in the timestamp bounds to the snapshot', () => {
2311+
const fakeTimestampBounds = {maxStaleness: 10};
2312+
2313+
database.getSnapshot(fakeTimestampBounds, err => {
2314+
assert.strictEqual(err.code, 3);
2315+
assert.strictEqual(
2316+
err.message,
2317+
'maxStaleness / minReadTimestamp is not supported for multi-use read-only transactions.'
2318+
);
2319+
});
2320+
});
2321+
2322+
it('should throw error if minReadTimestamp is passed in the timestamp bounds to the snapshot', () => {
2323+
const fakeTimestampBounds = {minReadTimestamp: 10};
2324+
2325+
database.getSnapshot(fakeTimestampBounds, err => {
2326+
assert.strictEqual(err.code, 3);
2327+
assert.strictEqual(
2328+
err.message,
2329+
'maxStaleness / minReadTimestamp is not supported for multi-use read-only transactions.'
2330+
);
2331+
});
2332+
});
2333+
2334+
it('should pass when maxStaleness is undefined', () => {
2335+
const fakeTimestampBounds = {minReadTimestamp: undefined};
2336+
2337+
database.getSnapshot(fakeTimestampBounds, assert.ifError);
2338+
2339+
const bounds = snapshotStub.lastCall.args[0];
2340+
assert.strictEqual(bounds, fakeTimestampBounds);
2341+
});
2342+
23102343
it('should begin a snapshot', () => {
23112344
beginSnapshotStub.callsFake(() => {});
23122345

0 commit comments

Comments
 (0)