diff --git a/google-cloud-bigquery/pom.xml b/google-cloud-bigquery/pom.xml index c59d4be0d..7928c70d0 100644 --- a/google-cloud-bigquery/pom.xml +++ b/google-cloud-bigquery/pom.xml @@ -50,6 +50,10 @@ org.checkerframework checker-qual + + com.google.auth + google-auth-library-credentials + com.google.auth google-auth-library-oauth2-http diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java index 841a790ee..dca129bfb 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java @@ -19,6 +19,7 @@ import static java.net.HttpURLConnection.HTTP_CREATED; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static java.net.HttpURLConnection.HTTP_OK; +import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.GenericUrl; @@ -116,7 +117,11 @@ private static BigQueryException translate(IOException exception) { private void validateRPC() throws BigQueryException, IOException { if (!this.options.hasValidUniverseDomain()) { - throw new BigQueryException(BigQueryException.UNKNOWN_CODE, "Invalid universe domain"); + String errorMessage = + String.format( + "The configured universe domain %s does not match the universe domain found in the credentials %s. If you haven't configured the universe domain explicitly, `googleapis.com` is the default.", + this.options.getUniverseDomain(), this.options.getCredentials().getUniverseDomain()); + throw new BigQueryException(HTTP_UNAUTHORIZED, errorMessage); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java index ba30ea0d4..eed5a6c61 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java @@ -19,6 +19,7 @@ import static com.google.cloud.bigquery.JobStatus.State.DONE; import static com.google.common.truth.Truth.assertThat; import static java.lang.System.currentTimeMillis; +import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -6402,8 +6403,12 @@ public void testUniverseDomainWithInvalidUniverseDomain() { bigQuery.listDatasets("bigquery-public-data"); fail("RPCs to invalid universe domain should fail"); } catch (BigQueryException e) { + assertEquals(e.getCode(), HTTP_UNAUTHORIZED); assertNotNull(e.getMessage()); - assertThat((e.getMessage().contains("Invalid universe domain"))).isTrue(); + assertThat( + (e.getMessage() + .contains("does not match the universe domain found in the credentials"))) + .isTrue(); } } @@ -6423,8 +6428,12 @@ public void testInvalidUniverseDomainWithMismatchCredentials() { bigQuery.listDatasets("bigquery-public-data"); fail("RPCs to invalid universe domain should fail"); } catch (BigQueryException e) { + assertEquals(e.getCode(), HTTP_UNAUTHORIZED); assertNotNull(e.getMessage()); - assertThat((e.getMessage().contains("Invalid universe domain"))).isTrue(); + assertThat( + (e.getMessage() + .contains("does not match the universe domain found in the credentials"))) + .isTrue(); } }