|
25 | 25 | import com.google.api.gax.retrying.TimedAttemptSettings;
|
26 | 26 | import com.google.api.gax.retrying.TimedRetryAlgorithm;
|
27 | 27 | import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext;
|
| 28 | +import com.google.gson.JsonObject; |
| 29 | +import com.google.gson.JsonParser; |
28 | 30 | import java.util.Iterator;
|
29 | 31 | import java.util.UUID;
|
30 | 32 | import java.util.concurrent.CancellationException;
|
@@ -107,9 +109,10 @@ private boolean shouldRetryBasedOnBigQueryRetryConfig(
|
107 | 109 | /*
|
108 | 110 | In some cases error messages may come without an exception
|
109 | 111 | e.g. status code 200 with a rate limit exceeded for job create
|
110 |
| - in these cases there is now previousThrowable so we need to check previousResponse |
| 112 | + in these cases there is no previousThrowable so we need |
| 113 | + to check for error messages in previousResponse |
111 | 114 | */
|
112 |
| - errorDesc = previousResponse.toString(); |
| 115 | + errorDesc = getErrorDescFromResponse(previousResponse); |
113 | 116 | }
|
114 | 117 |
|
115 | 118 | if (errorDesc != null) {
|
@@ -212,4 +215,28 @@ private TimedAttemptSettings createNextAttemptBasedOnTiming(
|
212 | 215 | }
|
213 | 216 | return getTimedAlgorithm().createNextAttempt(previousSettings);
|
214 | 217 | }
|
| 218 | + |
| 219 | + private String getErrorDescFromResponse(ResponseT previousResponse) { |
| 220 | + /* |
| 221 | + error messages may come without an exception and must be extracted from response |
| 222 | + following logic based on response body of jobs.insert method, so far the only |
| 223 | + known case where a response with status code 200 may contain an error message |
| 224 | + */ |
| 225 | + try { |
| 226 | + JsonObject responseJson = |
| 227 | + JsonParser.parseString(previousResponse.toString()).getAsJsonObject(); |
| 228 | + if (responseJson.has("status") && responseJson.getAsJsonObject("status").has("errorResult")) { |
| 229 | + return responseJson |
| 230 | + .getAsJsonObject("status") |
| 231 | + .getAsJsonObject("errorResult") |
| 232 | + .get("message") |
| 233 | + .toString(); |
| 234 | + } else { |
| 235 | + return null; |
| 236 | + } |
| 237 | + } catch (Exception e) { |
| 238 | + // exceptions here implies no error message present in response, returning null |
| 239 | + return null; |
| 240 | + } |
| 241 | + } |
215 | 242 | }
|
0 commit comments