diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index e30b0ffd2156..7483051b760e 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -293,6 +293,9 @@ public com.google.api.services.bigquery.model.Dataset call() { getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); + if (getOptions().getThrowNotFound() && answer == null) { + throw new BigQueryException(404, "Dataset not found"); + } return answer == null ? null : Dataset.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); @@ -482,6 +485,9 @@ public com.google.api.services.bigquery.model.Table call() { getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); + if (getOptions().getThrowNotFound() && answer == null) { + throw new BigQueryException(404, "Table not found"); + } return answer == null ? null : Table.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); @@ -696,6 +702,9 @@ public com.google.api.services.bigquery.model.Job call() { getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); + if (getOptions().getThrowNotFound() && answer == null) { + throw new BigQueryException(404, "Job not found"); + } return answer == null ? null : Job.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index 080bb57521f3..9119ed14315f 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -33,6 +33,7 @@ public class BigQueryOptions extends ServiceOptions { private static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery"; private static final Set SCOPES = ImmutableSet.of(BIGQUERY_SCOPE); private static final long serialVersionUID = -2437598817433266049L; + private boolean setThrowNotFound; public static class DefaultBigQueryFactory implements BigQueryFactory { @@ -112,6 +113,14 @@ protected BigQueryRpc getBigQueryRpcV2() { return (BigQueryRpc) getRpc(); } + public void setThrowNotFound(boolean setThrowNotFound) { + this.setThrowNotFound = setThrowNotFound; + } + + public boolean getThrowNotFound() { + return setThrowNotFound; + } + @SuppressWarnings("unchecked") @Override public Builder toBuilder() { diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 9b67445acd13..0a1b4bc88354 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -355,6 +355,17 @@ public void testGetDataset() { new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } + @Test + public void testGetDatasetNotFound() { + EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS)) + .andThrow(new BigQueryException(404, "Dataset not found")); + EasyMock.replay(bigqueryRpcMock); + options.setThrowNotFound(true); + bigquery = options.getService(); + thrown.expect(BigQueryException.class); + bigquery.getDataset("dataset-not-found"); + } + @Test public void testGetDatasetFromDatasetId() { EasyMock.expect(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) @@ -592,6 +603,18 @@ public void testGetTable() { assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); } + @Test + public void testGetTableNotFound() { + EasyMock.expect( + bigqueryRpcMock.getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS)) + .andThrow(new BigQueryException(404, "Table not found")); + EasyMock.replay(bigqueryRpcMock); + options.setThrowNotFound(true); + bigquery = options.getService(); + thrown.expect(BigQueryException.class); + bigquery.getTable(DATASET, "table-not-found"); + } + @Test public void testGetTableFromTableId() { EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) @@ -1179,6 +1202,17 @@ public void testGetJob() { assertEquals(new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_COPY_JOB)), job); } + @Test + public void testGetJobNotFound() { + EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS)) + .andThrow(new BigQueryException(404, "Job not found")); + EasyMock.replay(bigqueryRpcMock); + options.setThrowNotFound(true); + bigquery = options.getService(); + thrown.expect(BigQueryException.class); + bigquery.getJob("job-not-found"); + } + @Test public void testGetJobFromJobId() { EasyMock.expect(bigqueryRpcMock.getJob(PROJECT, JOB, null, EMPTY_RPC_OPTIONS))