我在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 Docs,isSuccess
函数确定成功回调:
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/