From 0ae2be8512888ba6c9b29af786fe5c20fcbdd9c3 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 6 Jan 2016 13:25:18 +0100 Subject: [PATCH] Add BigQueryError field to BigQueryException and tests --- .../google/gcloud/bigquery/BigQueryError.java | 14 +++++--- .../gcloud/bigquery/BigQueryException.java | 15 ++++++++ .../google/gcloud/spi/DefaultBigQueryRpc.java | 10 +++++- .../gcloud/bigquery/ITBigQueryTest.java | 36 +++++++++++++++++-- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryError.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryError.java index 2d89bccf62ea..e58f0d0b7213 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryError.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryError.java @@ -8,8 +8,14 @@ import java.util.Objects; /** - * Google Cloud BigQuery Job Error. Objects of this class represent errors occurred during the - * execution of a BigQuery Job. + * Google Cloud BigQuery Error. Objects of this class represent errors encountered by the BigQuery + * service while executing a request. A BigQuery Job that terminated with an error has a non-null + * {@link JobStatus#error()}. A job can also encounter errors during its execution that do not cause + * the whole job to fail (see {@link JobStatus#executionErrors()}). Similarly, queries and insert + * all requests can cause BigQuery errors that do not mean the whole operation failed (see + * {@link QueryResponse#executionErrors()} and {@link InsertAllResponse#insertErrors()}). When a + * {@link BigQueryException} is thrown the BigQuery Error that caused it, if any, can be accessed + * with {@link BigQueryException#error()}. */ public class BigQueryError implements Serializable { @@ -34,14 +40,14 @@ public ErrorProto apply(BigQueryError error) { private final String debugInfo; private final String message; - BigQueryError(String reason, String location, String message, String debugInfo) { + public BigQueryError(String reason, String location, String message, String debugInfo) { this.reason = reason; this.location = location; this.debugInfo = debugInfo; this.message = message; } - BigQueryError(String reason, String location, String message) { + public BigQueryError(String reason, String location, String message) { this.reason = reason; this.location = location; this.message = message; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java index 020917762fa3..e92ffacd8f09 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java @@ -31,8 +31,23 @@ public class BigQueryException extends BaseServiceException { private static final long serialVersionUID = -5504832700512784654L; public static final int UNKNOWN_CODE = -1; + private final BigQueryError error; + public BigQueryException(int code, String message, boolean retryable) { + this(code, message, retryable, null); + } + + public BigQueryException(int code, String message, boolean retryable, BigQueryError error) { super(code, message, retryable); + this.error = error; + } + + /** + * Returns the {@link BigQueryError} that caused this exception. Returns {@code null} if none + * exists. + */ + public BigQueryError error() { + return error; } /** diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java index 74d1c038a6bc..04e481b345c2 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java @@ -49,6 +49,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; +import com.google.gcloud.bigquery.BigQueryError; import com.google.gcloud.bigquery.BigQueryException; import com.google.gcloud.bigquery.BigQueryOptions; @@ -91,7 +92,14 @@ private static BigQueryException translate(IOException exception) { private static BigQueryException translate(GoogleJsonError exception) { boolean retryable = RETRYABLE_CODES.contains(exception.getCode()); - return new BigQueryException(exception.getCode(), exception.getMessage(), retryable); + BigQueryError bigqueryError = null; + if (exception.getErrors() != null && !exception.getErrors().isEmpty()) { + GoogleJsonError.ErrorInfo error = exception.getErrors().get(0); + bigqueryError = new BigQueryError(error.getReason(), error.getLocation(), error.getMessage(), + (String) error.get("debugInfo")); + } + return new BigQueryException(exception.getCode(), exception.getMessage(), retryable, + bigqueryError); } @Override diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java index 798973149d20..8742fd88025e 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -242,6 +243,11 @@ public void testUpdateDatasetWithSelectedFields() { assertTrue(bigquery.delete(OTHER_DATASET)); } + @Test + public void testGetNonExistingTable() { + assertNull(bigquery.getTable(DATASET, "test_get_non_existing_table")); + } + @Test public void testCreateAndGetTable() { String tableName = "test_create_and_get_table"; @@ -411,7 +417,7 @@ public void testListTables() { } @Test - public void testUdpateTable() { + public void testUpdateTable() { String tableName = "test_update_table"; BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); BaseTableInfo createdTableInfo = bigquery.create(tableInfo); @@ -426,7 +432,7 @@ public void testUdpateTable() { } @Test - public void testUdpateTableWithSelectedFields() { + public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); BaseTableInfo createdTableInfo = bigquery.create(tableInfo); @@ -444,6 +450,27 @@ public void testUdpateTableWithSelectedFields() { assertTrue(bigquery.delete(DATASET, tableName)); } + @Test + public void testUpdateNonExistingTable() { + TableInfo tableInfo = + TableInfo.of(TableId.of(DATASET, "test_update_non_existing_table"), SIMPLE_SCHEMA); + try { + bigquery.update(tableInfo); + fail("BigQueryException was expected"); + } catch (BigQueryException e) { + BigQueryError error = e.error(); + assertNotNull(error); + assertEquals("notFound", error.reason()); + assertNotNull(error.message()); + assertNotNull(error.debugInfo()); + } + } + + @Test + public void testDeleteNonExistingTable() { + assertFalse(bigquery.delete(DATASET, "test_delete_non_existing_table")); + } + @Test public void testInsertAll() { String tableName = "test_insert_all_table"; @@ -822,4 +849,9 @@ public void testCancelJob() throws InterruptedException { } assertNull(remoteJob.status().error()); } + + @Test + public void testCancelNonExistingJob() throws InterruptedException { + assertFalse(bigquery.cancel("test_cancel_non_existing_job")); + } }