【问题标题】:How to deal with errors in the Dropbox java API如何处理 Dropbox java API 中的错误
【发布时间】:2017-12-30 18:59:43
【问题描述】:

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

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

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)中是否有类似的东西。 我一直在环顾四周,似乎没有,但我想确保在我进入 here 提到的极其专业和复杂的编码的兔子洞之前。如果可以的话,请给我一个如何正确处理这些异常的示例。

【问题讨论】:

  • Exception within instanceof-block 中有一个instanceof 是不好的风格。为GoogleJsonResponseException 编写一个自己的catch-block 会更简洁。
  • 我知道有一天我会再次检查我的代码,这只是它的第二次修订。
  • 您阅读过文档吗?
  • 我有但我无法理解它或如何从这些异常中得到有意义的错误,这是我试图处理的错误之一dropbox.github.io/dropbox-sdk-java/api-docs/v3.0.x/com/dropbox/…
  • 你可以从catch (ListFolderErrorException e)开始

标签: java dropbox-api


【解决方案1】:

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

Dropbox Java SDK 会自动将 HTTPS 响应中的结构化错误数据解析为原生类,您可以根据需要使用或多或少的特定性。结构化错误响应(通常是响应正文中的 JSON)提供的粒度远不止状态码。

例如,以下是您链接到的 my post 的 StackOverflow 文档中现在缺少的代码示例:

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

还有here's an example,它显示了如何检查特定错误情况(即该示例中的“路径”错误)。

这是处理这些异常的推荐方法。我不相信 SDK 提供了一种检索原始未解析错误响应的方法。 (如果你不想使用SDK,你可以自己直接拨打the HTTPS endpoints。)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
相关资源
最近更新 更多