Skip to content

Commit

Permalink
Merge pull request #526 from mziccard/bigquery-error-exception
Browse files Browse the repository at this point in the history
Add BigQueryError field to BigQueryException and tests
  • Loading branch information
aozarov committed Jan 7, 2016
2 parents d03e5a7 + 0ae2be8 commit f1a5cec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.assertNotNull;
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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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";
Expand Down Expand Up @@ -826,4 +853,9 @@ public void testCancelJob() throws InterruptedException {
}
assertNull(remoteJob.status().error());
}

@Test
public void testCancelNonExistingJob() throws InterruptedException {
assertFalse(bigquery.cancel("test_cancel_non_existing_job"));
}
}

0 comments on commit f1a5cec

Please sign in to comment.