2020import static com .google .gcloud .spi .BigQueryRpc .Option .START_INDEX ;
2121import static com .google .gcloud .spi .BigQueryRpc .Option .TIMEOUT ;
2222import static com .google .gcloud .spi .BigQueryRpc .Option .USER_IP ;
23+ import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
2324
2425import com .google .api .client .googleapis .json .GoogleJsonError ;
2526import 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