From 5a82c854b6549c82c905eba4905378b59cc88af4 Mon Sep 17 00:00:00 2001 From: Phong Chuong <147636638+PhongChuong@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:16:09 -0500 Subject: [PATCH] Feat: Update universe domain exception error code/message (#3113) * Feat: Update universe domain exception error code/message * Feat: Update universe domain exception error code/message --- google-cloud-bigquery/pom.xml | 4 ++++ .../cloud/bigquery/spi/v2/HttpBigQueryRpc.java | 7 ++++++- .../google/cloud/bigquery/it/ITBigQueryTest.java | 13 +++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/google-cloud-bigquery/pom.xml b/google-cloud-bigquery/pom.xml index 7ce9e586a..2bd74f8d3 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 8b5a45913..7652e65a5 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; @@ -6445,8 +6446,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(); } } @@ -6466,8 +6471,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(); } }