如何处理Dropbox Java API中的错误 [英] How to deal with errors in the Dropbox java API

查看:51
本文介绍了如何处理Dropbox Java API中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试开发与某些云服务交互的应用程序时,我发现Dropbox API for Java特别令人困惑。

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

具体如何查找HTTP错误。例如,使用Google Drive API,如果请求失败,则将引发IOException,但是您可以将该IOException解析为GoogleJsonResponseException,然后可以提取状态代码。

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();
   }
}

所以在Java Dropbox API中有类似的东西(特别是3.0.5)。
我一直在环顾四周,似乎没有,但是我想确保在进入非常专业和复杂的代码之前,先提到此处。如果可以,请给我一个例子,说明如何正确处理这些异常。

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.

推荐答案

[交叉链接以供参考: https ://www.dropboxforum.com/t5/API-support/How-to-get-error-responses-java-api/mp/258338#M14989 ]

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

Dropbox Java SDK自动将HTTPS响应中的结构化错误数据解析为本机类,您可以根据需要使用或多或少的特定性。结构化错误响应(响应主体中通常为JSON)提供的粒度比状态代码要多得多。

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.

例如,这是StackOverflow文档中现在缺少的代码示例来自我链接的帖子

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).

这是处理这些异常的推荐方法。我认为SDK无法提供检索原始未解析错误响应的方法。 (但是,如果您不想使用SDK,则可以调用 HTTPS端点直接自己。)

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.)

这篇关于如何处理Dropbox Java API中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆