I have a non-Android Java app uploading thousands of files, one after the other.
For each insert, I'm implementing exponential back off like so:
Random randomGenerator = new Random();
for(int i = 0; i < 5; i++) {
try {
return service.files().insert(body, mediaContent).execute();
} catch(GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 500
|| e.getDetails().getCode() == 501
|| e.getDetails().getCode() == 502
|| e.getDetails().getCode() == 503
|| e.getDetails().getCode() == 504
|| e.getDetails().getCode() == 403) {
Thread.sleep((1 << i) * 1000
+ randomGenerator.nextInt(1001));
} else {
// error besides 5xx
throw e;
}
} catch (HttpResponseException e) {
if (e.getStatusCode() == 500 || e.getStatusCode() == 501
|| e.getStatusCode() == 502 || e.getStatusCode() == 503
|| e.getStatusCode() == 504
|| e.getStatusCode() == 403) {
Thread.sleep((1 << i) * 1000
+ randomGenerator.nextInt(1001));
} else {
// error besides 5xx
throw e;
}
} catch (SocketTimeoutException e) {
Thread.sleep((1 << i) * 1000 + randomGenerator.nextInt(1001));
}
Here's the issue:
There have been multiple times in which my app catches a GoogleJsonResponseException, yet e.getDetails() returns null
thus I get null pointer exception when checking the error code.
per the google docs at http://javadoc.google-api-java-client.googlecode.com/hg/1.6.0-beta/com/google/api/client/googleapis/json/GoogleJsonResponseException.html#getDetails()
Returns the Google JSON error details or null for none (for example if response is not JSON).
So basically, I'm catching a GoogleJsonResponseException
but there is no JSON.
Why in the world can I catch a GoogleJsonResponseException
when there's not actually any Json associated with it? Why didn't it instead catch HttpResponseException
?
I want my app to be able to catch/use Json if it's returned, but I don't know how to do that reliably when GoogleJsonResponseException
isn't even guaranteed to return Json.
Any suggestions?
Should I just be using HttpResponseException
to reliably check statusCode and then looking at getMessage()
to see if there's JSON?
Or should I just use GoogleJsonResponseException
but call it's parent HttpResponseException
method getStatusCode()
to see status code, and only getDetails()
if I really want to check/dive into any possible Json?