Catching GoogleJsonResponseException but e.getDetails () returns null

I have a non Android Java application downloading thousands of files one by one.

For each insert, I perform an exponential shutdown 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 problem:

There have been multiple times when my application catches a GoogleJsonResponseException, but e.getDetails () returns null

, so I get a null pointer exception when checking the error code.

per 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 was catching GoogleJsonResponseException

, but JSON doesn't exist.

Why in the world can I catch GoogleJsonResponseException

if there is actually no Json associated with it? Why didn't he catch HttpResponseException

?

I want my application to be able to catch / use Json if it returns, but I don't know how to do this reliably when GoogleJsonResponseException

it is not even guaranteed to return Json.

Any suggestions?

Should I just use HttpResponseException

to check the statusCode reliably and then look getMessage()

to see if there is JSON?

Or just use GoogleJsonResponseException

but call it the parent HttpResponseException

method getStatusCode()

to see the status code, and only getDetails()

if I really want to check / dive into any possible Json?

+3


source to share


1 answer


I'll post this as an answer, so it's there for those in the same position, although it doesn't address the specific questions you asked.

Several suggestions

  • If you upload more than 30 files, you will run into flow control issues. Flow control is impaired, so you should act voluntarily and not become a victim. See 403 speed limit on insertion sometimes succeeds and 403 speed limit after 1 insertion per second

  • Enable HTTP logging (see code.google.com pages for Java httpcliebnt to execute). This will at least allow you to see what the answer is and perhaps provide some hints on how best to catch the exception.

  • The page you linked to was for version 1.6. The afaik library is currently at level 1.19, so make sure you are using the latest library and the latest documentation.



What adds to the confusion is the three separate libraries.

+2


source







All Articles