3

While trying to develop an application that interacts with some cloud services I found the Dropbox API for Java to be especially confusing.

Specifically how to find the HTTP errors. For instance, with the Google Drive API if a request fails an IOException will be thrown however you can parse that IOException into a GoogleJsonResponseException which you can then extract the status code.

try {
    File f =drive.files().create(fileMetadata).setFields("id").execute();
    return f.getId();
} catch (IOException e) {
    if (e instanceof GoogleJsonResponseException){
          int statusCode = ((GoogleJsonResponseException) e).getStatusCode();
          errorHandler(statusCode);
   } else {
       e.printStackTrace();
   }
}

So is there something like this in the java dropbox API (Specifically 3.0.5). I have been looking around and it seems like no but I wanted to make sure before I go down the rabbit hole of extremely specialized and complex coding like mentioned here. If it is could someone please give me an example of how I could properly handle these exceptions.

7
  • Having an instanceof on an Exception within a catch-block is bad style. It would be much cleaner to write an own catch-block for the GoogleJsonResponseException.
    – Turing85
    Commented Dec 30, 2017 at 19:02
  • I know I am going to go over my code again one day, this is only its second revision.
    – Austin
    Commented Dec 30, 2017 at 19:04
  • Have you read the documentation? Commented Dec 30, 2017 at 19:26
  • I have and I can't understand it or how to get a meaningful error out of these exceptions, here is one of the errors I am trying to handle dropbox.github.io/dropbox-sdk-java/api-docs/v3.0.x/com/dropbox/…
    – Austin
    Commented Dec 30, 2017 at 19:30
  • You can start with catch (ListFolderErrorException e) Commented Dec 30, 2017 at 19:33

1 Answer 1

1

[Cross-linking for reference: https://www.dropboxforum.com/t5/API-support/How-to-get-error-responses-java-api/m-p/258338#M14989 ]

The Dropbox Java SDK automatically parses out the structured error data from the HTTPS response into native classes, and you can use as much or little of that specificity as you want. The structured error responses (generally JSON in the response body) offer much more granularity than just status codes.

For example, here's the code sample that is now missing from StackOverflow documentation from my post that you linked to:

try {
    SharedLinkMetadata sharedLinkMetadata = client.sharing().createSharedLinkWithSettings("/test.txt");
    System.out.println(sharedLinkMetadata.getUrl());
} catch (CreateSharedLinkWithSettingsErrorException ex) {
    System.out.println(ex);
} catch (DbxException ex) {
    System.out.println(ex);
}

And here's an example that shows how to check for a particular error case (i.e., a "path" error in that example).

That's the recommended way of handling these exceptions. I don't believe the SDK offers a way to retrieve the original unparsed error response. (If you don't want to use the SDK though, you can call the HTTPS endpoints yourself directly.)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.