Skip to content

Commit e3a99c2

Browse files
committed
Add error codes, handle NOT_FOUND in BigQuery RPC, add javadoc
1 parent 381d056 commit e3a99c2

2 files changed

Lines changed: 75 additions & 14 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,57 @@ public Y y() {
9898
}
9999
}
100100

101+
/**
102+
* Returns the requested dataset or {@code null} if not found.
103+
*
104+
* @throws BigQueryException upon failure
105+
*/
101106
Dataset getDataset(String datasetId, Map<Option, ?> options) throws BigQueryException;
102107

108+
/**
109+
* Lists the project's datasets. Partial information is returned on a dataset (datasetReference,
110+
* friendlyName and id). To get full information use {@link #getDataset(String, Map)}.
111+
*
112+
* @throws BigQueryException upon failure
113+
*/
103114
Tuple<String, Iterable<Dataset>> listDatasets(Map<Option, ?> options) throws BigQueryException;
104115

105116
Dataset create(Dataset dataset, Map<Option, ?> options) throws BigQueryException;
106117

118+
/**
119+
* Delete the requested dataset.
120+
*
121+
* @return {@code true} if dataset was deleted, {@code false} if it was not found
122+
* @throws BigQueryException upon failure
123+
*/
107124
boolean deleteDataset(String datasetId, Map<Option, ?> options) throws BigQueryException;
108125

109126
Dataset patch(Dataset dataset, Map<Option, ?> options) throws BigQueryException;
110127

128+
/**
129+
* Returns the requested table or {@code null} if not found.
130+
*
131+
* @throws BigQueryException upon failure
132+
*/
111133
Table getTable(String datasetId, String tableId, Map<Option, ?> options) throws BigQueryException;
112134

135+
/**
136+
* Lists the dataset's tables. Partial information is returned on a table (tableReference,
137+
* friendlyName, id and type). To get full information use {@link #getTable(String, String, Map)}.
138+
*
139+
* @throws BigQueryException upon failure
140+
*/
113141
Tuple<String, Iterable<Table>> listTables(String dataset, Map<Option, ?> options)
114142
throws BigQueryException;
115143

116144
Table create(Table table, Map<Option, ?> options) throws BigQueryException;
117145

146+
/**
147+
* Delete the requested table.
148+
*
149+
* @return {@code true} if table was deleted, {@code false} if it was not found
150+
* @throws BigQueryException upon failure
151+
*/
118152
boolean deleteTable(String datasetId, String tableId, Map<Option, ?> options)
119153
throws BigQueryException;
120154

@@ -126,8 +160,18 @@ TableDataInsertAllResponse insertAll(TableReference table, TableDataInsertAllReq
126160
Tuple<String, Iterable<TableRow>> listTableData(String datasetId, String tableId,
127161
Map<Option, ?> options) throws BigQueryException;
128162

163+
/**
164+
* Returns the requested job or {@code null} if not found.
165+
*
166+
* @throws BigQueryException upon failure
167+
*/
129168
Job getJob(String jobId, Map<Option, ?> options) throws BigQueryException;
130169

170+
/**
171+
* Lists the project's jobs.
172+
*
173+
* @throws BigQueryException upon failure
174+
*/
131175
Tuple<String, Iterable<Job>> listJobs(Map<Option, ?> options) throws BigQueryException;
132176

133177
Job create(Job job, Map<Option, ?> options) throws BigQueryException;

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.gcloud.spi.BigQueryRpc.Option.START_INDEX;
2121
import static com.google.gcloud.spi.BigQueryRpc.Option.TIMEOUT;
2222
import static com.google.gcloud.spi.BigQueryRpc.Option.USER_IP;
23+
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
2324

2425
import com.google.api.client.googleapis.json.GoogleJsonError;
2526
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
@@ -64,7 +65,7 @@ public class DefaultBigQueryRpc implements BigQueryRpc {
6465

6566
public static final String DEFAULT_PROJECTION = "full";
6667
// see: https://cloud.google.com/bigquery/troubleshooting-errors
67-
private static final Set<Integer> RETRYABLE_CODES = ImmutableSet.of(503);
68+
private static final Set<Integer> RETRYABLE_CODES = ImmutableSet.of(500, 502, 503, 504);
6869
private final BigQueryOptions options;
6970
private final Bigquery bigquery;
7071

@@ -80,7 +81,8 @@ public DefaultBigQueryRpc(BigQueryOptions options) {
8081

8182
private static BigQueryException translate(IOException exception) {
8283
BigQueryException translated;
83-
if (exception instanceof GoogleJsonResponseException) {
84+
if (exception instanceof GoogleJsonResponseException
85+
&& ((GoogleJsonResponseException) exception).getDetails() != null) {
8486
translated = translate(((GoogleJsonResponseException) exception).getDetails());
8587
} else {
8688
translated =
@@ -91,8 +93,7 @@ private static BigQueryException translate(IOException exception) {
9193
}
9294

9395
private static BigQueryException translate(GoogleJsonError exception) {
94-
boolean retryable = RETRYABLE_CODES.contains(exception.getCode())
95-
|| "InternalError".equals(exception.getMessage());
96+
boolean retryable = RETRYABLE_CODES.contains(exception.getCode());
9697
return new BigQueryException(exception.getCode(), exception.getMessage(), retryable);
9798
}
9899

@@ -105,8 +106,12 @@ public Dataset getDataset(String datasetId, Map<Option, ?> options) throws BigQu
105106
.setQuotaUser(QUOTA_USER.getString(options))
106107
.setUserIp(USER_IP.getString(options))
107108
.execute();
108-
} catch (IOException ex) {
109-
throw translate(ex);
109+
} catch(IOException ex) {
110+
BigQueryException serviceException = translate(ex);
111+
if (serviceException.code() == HTTP_NOT_FOUND) {
112+
return null;
113+
}
114+
throw serviceException;
110115
}
111116
}
112117

@@ -164,7 +169,7 @@ public boolean deleteDataset(String datasetId, Map<Option, ?> options) throws Bi
164169
return true;
165170
} catch (IOException ex) {
166171
BigQueryException serviceException = translate(ex);
167-
if (serviceException.code() == 404) {
172+
if (serviceException.code() == HTTP_NOT_FOUND) {
168173
return false;
169174
}
170175
throw serviceException;
@@ -195,8 +200,12 @@ public Table getTable(String datasetId, String tableId, Map<Option, ?> options)
195200
.setQuotaUser(QUOTA_USER.getString(options))
196201
.setUserIp(USER_IP.getString(options))
197202
.execute();
198-
} catch (IOException ex) {
199-
throw translate(ex);
203+
} catch(IOException ex) {
204+
BigQueryException serviceException = translate(ex);
205+
if (serviceException.code() == HTTP_NOT_FOUND) {
206+
return null;
207+
}
208+
throw serviceException;
200209
}
201210
}
202211

@@ -320,8 +329,12 @@ public Job getJob(String jobId, Map<Option, ?> options) throws BigQueryException
320329
.setQuotaUser(QUOTA_USER.getString(options))
321330
.setUserIp(USER_IP.getString(options))
322331
.execute();
323-
} catch (IOException ex) {
324-
throw translate(ex);
332+
} catch(IOException ex) {
333+
BigQueryException serviceException = translate(ex);
334+
if (serviceException.code() == HTTP_NOT_FOUND) {
335+
return null;
336+
}
337+
throw serviceException;
325338
}
326339
}
327340

@@ -381,7 +394,7 @@ public boolean cancel(String jobId, Map<Option, ?> options) throws BigQueryExcep
381394
return true;
382395
} catch (IOException ex) {
383396
BigQueryException serviceException = translate(ex);
384-
if (serviceException.code() == 404) {
397+
if (serviceException.code() == HTTP_NOT_FOUND) {
385398
return false;
386399
}
387400
throw serviceException;
@@ -401,8 +414,12 @@ public GetQueryResultsResponse getQueryResults(JobReference job, Map<Option, ?>
401414
BigInteger.valueOf(START_INDEX.getLong(options)) : null)
402415
.setTimeoutMs(TIMEOUT.getLong(options))
403416
.execute();
404-
} catch (IOException ex) {
405-
throw translate(ex);
417+
} catch(IOException ex) {
418+
BigQueryException serviceException = translate(ex);
419+
if (serviceException.code() == HTTP_NOT_FOUND) {
420+
return null;
421+
}
422+
throw serviceException;
406423
}
407424
}
408425

0 commit comments

Comments
 (0)