Skip to content

Commit e380520

Browse files
Praful Makanisduskis
authored andcommitted
---
yaml --- r: 15323 b: refs/heads/autosynth-bigtable c: 786ce15 h: refs/heads/master i: 15321: f2a7812 15319: 55482c9
1 parent c690aca commit e380520

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ refs/heads/autosynth-asset: cd8251de8c40e239ad24dcf9ed93ea2708a3eed5
126126
refs/heads/autosynth-automl: cced2f56bbef0499609073edbca6253e1df5e535
127127
refs/heads/autosynth-bigquerydatatransfer: a6667617707608b1dbfb02d59c22b5152f208da7
128128
refs/heads/autosynth-bigquerystorage: 14ab055598b943ae3f33f484e9fb1653355d08e7
129-
refs/heads/autosynth-bigtable: c8e0f2f97ffc116b43a682a9a026f9c70ca0b9f5
129+
refs/heads/autosynth-bigtable: 786ce1585f78c4dce600ab72caa6407739e8361e
130130
refs/heads/autosynth-bigtable-admin: 6379a2bc712f2736c83de0e009b4d26da4fa82ca
131131
refs/heads/autosynth-containeranalysis: 781fdb430a60f9a6491f116e31e4e10118157bdb
132132
refs/heads/autosynth-datastore: af1fb76aa3eee02fe6f31f8fa1c72a4f048d149b

branches/autosynth-bigtable/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.cloud.RetryHelper.runWithRetries;
2020
import static com.google.common.base.Preconditions.checkArgument;
21+
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
2122

2223
import com.google.api.core.InternalApi;
2324
import com.google.api.gax.paging.Page;
@@ -293,6 +294,9 @@ public com.google.api.services.bigquery.model.Dataset call() {
293294
getOptions().getRetrySettings(),
294295
EXCEPTION_HANDLER,
295296
getOptions().getClock());
297+
if (getOptions().getThrowNotFound() && answer == null) {
298+
throw new BigQueryException(HTTP_NOT_FOUND, "Dataset not found");
299+
}
296300
return answer == null ? null : Dataset.fromPb(this, answer);
297301
} catch (RetryHelper.RetryHelperException e) {
298302
throw BigQueryException.translateAndThrow(e);
@@ -482,6 +486,9 @@ public com.google.api.services.bigquery.model.Table call() {
482486
getOptions().getRetrySettings(),
483487
EXCEPTION_HANDLER,
484488
getOptions().getClock());
489+
if (getOptions().getThrowNotFound() && answer == null) {
490+
throw new BigQueryException(HTTP_NOT_FOUND, "Table not found");
491+
}
485492
return answer == null ? null : Table.fromPb(this, answer);
486493
} catch (RetryHelper.RetryHelperException e) {
487494
throw BigQueryException.translateAndThrow(e);
@@ -702,6 +709,9 @@ public com.google.api.services.bigquery.model.Job call() {
702709
getOptions().getRetrySettings(),
703710
EXCEPTION_HANDLER,
704711
getOptions().getClock());
712+
if (getOptions().getThrowNotFound() && answer == null) {
713+
throw new BigQueryException(HTTP_NOT_FOUND, "Job not found");
714+
}
705715
return answer == null ? null : Job.fromPb(this, answer);
706716
} catch (RetryHelper.RetryHelperException e) {
707717
throw BigQueryException.translateAndThrow(e);

branches/autosynth-bigtable/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class BigQueryOptions extends ServiceOptions<BigQuery, BigQueryOptions> {
3434
private static final Set<String> SCOPES = ImmutableSet.of(BIGQUERY_SCOPE);
3535
private static final long serialVersionUID = -2437598817433266049L;
3636
private final String location;
37+
// set the option ThrowNotFound when you want to throw the exception when the value not found
38+
private boolean setThrowNotFound;
3739

3840
public static class DefaultBigQueryFactory implements BigQueryFactory {
3941

@@ -125,6 +127,14 @@ public String getLocation() {
125127
return location;
126128
}
127129

130+
public void setThrowNotFound(boolean setThrowNotFound) {
131+
this.setThrowNotFound = setThrowNotFound;
132+
}
133+
134+
public boolean getThrowNotFound() {
135+
return setThrowNotFound;
136+
}
137+
128138
@SuppressWarnings("unchecked")
129139
@Override
130140
public Builder toBuilder() {

branches/autosynth-bigtable/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,29 @@ public void testGetDataset() {
366366
new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset);
367367
}
368368

369+
@Test
370+
public void testGetDatasetNotFoundWhenThrowIsDisabled() {
371+
EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS))
372+
.andReturn(DATASET_INFO_WITH_PROJECT.toPb());
373+
EasyMock.replay(bigqueryRpcMock);
374+
options.setThrowNotFound(false);
375+
bigquery = options.getService();
376+
Dataset dataset = bigquery.getDataset(DATASET);
377+
assertEquals(
378+
new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset);
379+
}
380+
381+
@Test
382+
public void testGetDatasetNotFoundWhenThrowIsEnabled() {
383+
EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS))
384+
.andThrow(new BigQueryException(404, "Dataset not found"));
385+
EasyMock.replay(bigqueryRpcMock);
386+
options.setThrowNotFound(true);
387+
bigquery = options.getService();
388+
thrown.expect(BigQueryException.class);
389+
bigquery.getDataset("dataset-not-found");
390+
}
391+
369392
@Test
370393
public void testGetDatasetFromDatasetId() {
371394
EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS))
@@ -603,6 +626,29 @@ public void testGetTable() {
603626
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table);
604627
}
605628

629+
@Test
630+
public void testGetTableNotFoundWhenThrowIsDisabled() {
631+
EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))
632+
.andReturn(TABLE_INFO_WITH_PROJECT.toPb());
633+
EasyMock.replay(bigqueryRpcMock);
634+
options.setThrowNotFound(false);
635+
bigquery = options.getService();
636+
Table table = bigquery.getTable(DATASET, TABLE);
637+
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table);
638+
}
639+
640+
@Test
641+
public void testGetTableNotFoundWhenThrowIsEnabled() {
642+
EasyMock.expect(
643+
bigqueryRpcMock.getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS))
644+
.andThrow(new BigQueryException(404, "Table not found"));
645+
EasyMock.replay(bigqueryRpcMock);
646+
options.setThrowNotFound(true);
647+
bigquery = options.getService();
648+
thrown.expect(BigQueryException.class);
649+
bigquery.getTable(DATASET, "table-not-found");
650+
}
651+
606652
@Test
607653
public void testGetTableFromTableId() {
608654
EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))
@@ -1201,6 +1247,28 @@ public void testGetJobWithLocation() {
12011247
assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job);
12021248
}
12031249

1250+
@Test
1251+
public void testGetJobNotFoundWhenThrowIsDisabled() {
1252+
EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS))
1253+
.andReturn(COMPLETE_COPY_JOB.toPb());
1254+
EasyMock.replay(bigqueryRpcMock);
1255+
options.setThrowNotFound(false);
1256+
bigquery = options.getService();
1257+
Job job = bigquery.getJob(JOB);
1258+
assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job);
1259+
}
1260+
1261+
@Test
1262+
public void testGetJobNotFoundWhenThrowIsEnabled() {
1263+
EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS))
1264+
.andThrow(new BigQueryException(404, "Job not found"));
1265+
EasyMock.replay(bigqueryRpcMock);
1266+
options.setThrowNotFound(true);
1267+
bigquery = options.getService();
1268+
thrown.expect(BigQueryException.class);
1269+
bigquery.getJob("job-not-found");
1270+
}
1271+
12041272
@Test
12051273
public void testGetJobFromJobId() {
12061274
EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS))

0 commit comments

Comments
 (0)