Skip to content

Commit f77cae7

Browse files
Praful Makanisduskis
authored andcommitted
---
yaml --- r: 20347 b: refs/heads/autosynth-scheduler c: 786ce15 h: refs/heads/master i: 20345: 788426a 20343: 6302a40
1 parent cb7b713 commit f77cae7

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
@@ -137,7 +137,7 @@ refs/heads/autosynth-kms: 2828edfe3d2c53dd6e71912eae8a53c87bf40c87
137137
refs/heads/autosynth-language: c3d990dd34d81e7e935041e7147fb9dd27f8a557
138138
refs/heads/autosynth-os-login: 092fdbed6d5317948f92b708e9f50dedd89fc666
139139
refs/heads/autosynth-redis: 9e1fe503973c7b4a9ba26afb1fcddc2a57ba795a
140-
refs/heads/autosynth-scheduler: c8e0f2f97ffc116b43a682a9a026f9c70ca0b9f5
140+
refs/heads/autosynth-scheduler: 786ce1585f78c4dce600ab72caa6407739e8361e
141141
refs/heads/autosynth-spanner: 9bff86d057df31e04c76d72865e8e073ac5794fb
142142
refs/heads/autosynth-speech: 75d6c62a9d07d3a3642980502a25d07fbde0f232
143143
refs/heads/autosynth-tasks: b0cdb991f3f75345151a3f68db1aab273dfc069b

branches/autosynth-scheduler/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-scheduler/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-scheduler/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)