Skip to content

Commit

Permalink
Feat: Update universe domain exception error code/message (#3113)
Browse files Browse the repository at this point in the history
* Feat: Update universe domain exception error code/message

* Feat: Update universe domain exception error code/message
  • Loading branch information
PhongChuong committed Feb 6, 2024
1 parent 2156f02 commit 5a82c85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions google-cloud-bigquery/pom.xml
Expand Up @@ -50,6 +50,10 @@
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}

Expand Down

0 comments on commit 5a82c85

Please sign in to comment.