android - 在Android上检索错误对象而不是HTTP 400

标签 android error-handling graphql apollo-client

我在Android上使用apollo client以通过授权使用graphql服务。

当该认证 token 过期时,将显示错误:HTTP 400而不是实际的服务器错误:

{
  "errors": [
    {
      "message": "Invalid token.",
      ...
    }
  ]
}

从技术上讲,这应该执行onSuccess,但应执行hasErrors而不是onFailure
override fun onFailure(e: ApolloException) {
    //I receive the error here. 
}

override fun onResponse(response: Response<T>) {
    if (!response.hasErrors()) {
        //Success
    } else {
        //I should receive the error here...
    }
}

这和customType有关吗?

最佳答案

实际上,Apollo与Retrofit非常相似,后者均使用 OkHttp 来处理响应。 Apollo处理HTTP响应的方式遵循OkHttp规则和行为。根据OkHttp DocsisSuccess函数确定成功回调:

Returns true if the code is in [200..300], which means the request was successfully received, understood, and accepted.



在Apollo中,这意味着只要状态代码不在200-300之间,它将总是进入onFailure回调(在您的情况下,您是由于HTTP 400才得到它)。

使代码进入onSuccess回调的唯一方法是在后端捕获异常,然后将HTTP 200与错误消息一起发送,而不是HTTP 400。我的团队做到了,而且行得通。

关于android - 在Android上检索错误对象而不是HTTP 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60731315/

相关文章:

Android Studio ,在 Windows : JAVA_HOME is not set and no 'java' command could be found in your PATH 上

javascript - 如何将变量从 node.js 函数传递到 graphql-tag?

exception - 哪种契约(Contract)(按契约(Contract)设计)更好?

javascript - 在 graphql 的解析器中获取用户代理访问权限

rest - 如何在 GraphQL 中对 "index"列表与 "show"详细信息进行建模?

java - Android - paint.setShadowLayer 忽略 shadowColor

android - 打开 android XML 文件在新安装的 Eclipse 中显示 NullpointerException

java - 如何使用 java/android 从 Firebase ONCE 读取数据?

iphone - 我应该为哪些核心数据错误做准备?

php - 如何处理重复条目的错误?