Skip to content

Commit f1a5cec

Browse files
committed
Merge pull request #526 from mziccard/bigquery-error-exception
Add BigQueryError field to BigQueryException and tests
2 parents d03e5a7 + 0ae2be8 commit f1a5cec

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryError.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
import java.util.Objects;
99

1010
/**
11-
* Google Cloud BigQuery Job Error. Objects of this class represent errors occurred during the
12-
* execution of a BigQuery Job.
11+
* Google Cloud BigQuery Error. Objects of this class represent errors encountered by the BigQuery
12+
* service while executing a request. A BigQuery Job that terminated with an error has a non-null
13+
* {@link JobStatus#error()}. A job can also encounter errors during its execution that do not cause
14+
* the whole job to fail (see {@link JobStatus#executionErrors()}). Similarly, queries and insert
15+
* all requests can cause BigQuery errors that do not mean the whole operation failed (see
16+
* {@link QueryResponse#executionErrors()} and {@link InsertAllResponse#insertErrors()}). When a
17+
* {@link BigQueryException} is thrown the BigQuery Error that caused it, if any, can be accessed
18+
* with {@link BigQueryException#error()}.
1319
*/
1420
public class BigQueryError implements Serializable {
1521

@@ -34,14 +40,14 @@ public ErrorProto apply(BigQueryError error) {
3440
private final String debugInfo;
3541
private final String message;
3642

37-
BigQueryError(String reason, String location, String message, String debugInfo) {
43+
public BigQueryError(String reason, String location, String message, String debugInfo) {
3844
this.reason = reason;
3945
this.location = location;
4046
this.debugInfo = debugInfo;
4147
this.message = message;
4248
}
4349

44-
BigQueryError(String reason, String location, String message) {
50+
public BigQueryError(String reason, String location, String message) {
4551
this.reason = reason;
4652
this.location = location;
4753
this.message = message;

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java

+15
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,23 @@ public class BigQueryException extends BaseServiceException {
3131
private static final long serialVersionUID = -5504832700512784654L;
3232
public static final int UNKNOWN_CODE = -1;
3333

34+
private final BigQueryError error;
35+
3436
public BigQueryException(int code, String message, boolean retryable) {
37+
this(code, message, retryable, null);
38+
}
39+
40+
public BigQueryException(int code, String message, boolean retryable, BigQueryError error) {
3541
super(code, message, retryable);
42+
this.error = error;
43+
}
44+
45+
/**
46+
* Returns the {@link BigQueryError} that caused this exception. Returns {@code null} if none
47+
* exists.
48+
*/
49+
public BigQueryError error() {
50+
return error;
3651
}
3752

3853
/**

gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.google.common.collect.ImmutableSet;
5050
import com.google.common.collect.Iterables;
5151

52+
import com.google.gcloud.bigquery.BigQueryError;
5253
import com.google.gcloud.bigquery.BigQueryException;
5354
import com.google.gcloud.bigquery.BigQueryOptions;
5455

@@ -91,7 +92,14 @@ private static BigQueryException translate(IOException exception) {
9192

9293
private static BigQueryException translate(GoogleJsonError exception) {
9394
boolean retryable = RETRYABLE_CODES.contains(exception.getCode());
94-
return new BigQueryException(exception.getCode(), exception.getMessage(), retryable);
95+
BigQueryError bigqueryError = null;
96+
if (exception.getErrors() != null && !exception.getErrors().isEmpty()) {
97+
GoogleJsonError.ErrorInfo error = exception.getErrors().get(0);
98+
bigqueryError = new BigQueryError(error.getReason(), error.getLocation(), error.getMessage(),
99+
(String) error.get("debugInfo"));
100+
}
101+
return new BigQueryException(exception.getCode(), exception.getMessage(), retryable,
102+
bigqueryError);
95103
}
96104

97105
@Override

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertNotNull;
2222
import static org.junit.Assert.assertNull;
2323
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.fail;
2425

2526
import com.google.common.collect.ImmutableList;
2627
import com.google.common.collect.ImmutableMap;
@@ -242,6 +243,11 @@ public void testUpdateDatasetWithSelectedFields() {
242243
assertTrue(bigquery.delete(OTHER_DATASET));
243244
}
244245

246+
@Test
247+
public void testGetNonExistingTable() {
248+
assertNull(bigquery.getTable(DATASET, "test_get_non_existing_table"));
249+
}
250+
245251
@Test
246252
public void testCreateAndGetTable() {
247253
String tableName = "test_create_and_get_table";
@@ -411,7 +417,7 @@ public void testListTables() {
411417
}
412418

413419
@Test
414-
public void testUdpateTable() {
420+
public void testUpdateTable() {
415421
String tableName = "test_update_table";
416422
BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA);
417423
BaseTableInfo createdTableInfo = bigquery.create(tableInfo);
@@ -426,7 +432,7 @@ public void testUdpateTable() {
426432
}
427433

428434
@Test
429-
public void testUdpateTableWithSelectedFields() {
435+
public void testUpdateTableWithSelectedFields() {
430436
String tableName = "test_update_with_selected_fields_table";
431437
BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA);
432438
BaseTableInfo createdTableInfo = bigquery.create(tableInfo);
@@ -444,6 +450,27 @@ public void testUdpateTableWithSelectedFields() {
444450
assertTrue(bigquery.delete(DATASET, tableName));
445451
}
446452

453+
@Test
454+
public void testUpdateNonExistingTable() {
455+
TableInfo tableInfo =
456+
TableInfo.of(TableId.of(DATASET, "test_update_non_existing_table"), SIMPLE_SCHEMA);
457+
try {
458+
bigquery.update(tableInfo);
459+
fail("BigQueryException was expected");
460+
} catch (BigQueryException e) {
461+
BigQueryError error = e.error();
462+
assertNotNull(error);
463+
assertEquals("notFound", error.reason());
464+
assertNotNull(error.message());
465+
assertNotNull(error.debugInfo());
466+
}
467+
}
468+
469+
@Test
470+
public void testDeleteNonExistingTable() {
471+
assertFalse(bigquery.delete(DATASET, "test_delete_non_existing_table"));
472+
}
473+
447474
@Test
448475
public void testInsertAll() {
449476
String tableName = "test_insert_all_table";
@@ -826,4 +853,9 @@ public void testCancelJob() throws InterruptedException {
826853
}
827854
assertNull(remoteJob.status().error());
828855
}
856+
857+
@Test
858+
public void testCancelNonExistingJob() throws InterruptedException {
859+
assertFalse(bigquery.cancel("test_cancel_non_existing_job"));
860+
}
829861
}

0 commit comments

Comments
 (0)