【问题标题】:Catching and handling errors with Apollo Client使用 Apollo Client 捕获和处理错误
【发布时间】:2018-03-05 05:00:09
【问题描述】:

我正在尝试以优雅的方式处理 Apollo 的错误。所以这个想法是每当用户遇到某种错误(500或其他任何错误)时。我只想显示一条消息,例如“出了点问题,请稍后再试”。

在组件的第一次渲染时,它工作正常(错误出现在 data.error 中)但是当我离开路由并且组件被卸载时,它会抛出。

目前我无法阻止它抛出错误。我已经尝试了该线程中的几乎所有内容:https://github.com/apollographql/react-apollo/issues/604#issuecomment-355648596

  • link-error:记录错误,但不阻止它抛出
  • hoc:记录错误,但不阻止它抛出
  • componentDidCatch: 什么都不做
  • errorPolicy:防止错误,但这意味着我也不能再处理它们了。

我总是得到以下堆栈: Unhandled error Network error: Response not successful: Received status code 500 Error: Network error: Response not successful: Received status code 500 at new ApolloError (webpack-internal:///./node_modules/apollo-client/errors/ApolloError.js:36:28) at eval (webpack-internal:///./node_modules/apollo-client/core/QueryManager.js:287:41) at eval (webpack-internal:///./node_modules/apollo-client/core/QueryManager.js:668:17) at Array.forEach (<anonymous>) at eval (webpack-internal:///./node_modules/apollo-client/core/QueryManager.js:667:18) at Map.forEach (<anonymous>) at QueryManager.broadcastQueries (webpack-internal:///./node_modules/apollo-client/core/QueryManager.js:662:22) at eval (webpack-internal:///./node_modules/apollo-client/core/QueryManager.js:236:31) at <anonymous>

供参考: - 阿波罗客户端:2.2.5 - 反应阿波罗:2.0.4

【问题讨论】:

  • 抛出错误是什么意思?你的应用会崩溃吗?
  • 是的,@Locco0_0,它崩溃了!

标签: reactjs apollo


【解决方案1】:

As described here:

我相信使用 graphql HOC 时“处理”错误的方法是 只需检查组件内的 data.errors。有一张支票 如果 data.errors 未被访问,则在 apollo-client 中抛出。

在 GitHub 上发表此评论的整个问题对如何和为什么进行了非常有趣的讨论。大多数建议的解决方案对我都不起作用,尤其是 componentDidCatch,恕我直言,这是最干净的替代方案。

我可以通过触摸this.props.data.error 克服它,但我绝对不满意。

编辑

我刚刚注意到您提到了卸载组件时发生的错误......好吧,it seems to be fixed on 2.1.0-beta.3。 :-)

【讨论】:

  • 正确,测试版修复了它!
猜你喜欢
  • 2017-09-24
  • 2018-09-02
  • 2021-03-11
  • 2018-08-01
  • 1970-01-01
  • 2018-07-09
  • 2018-10-21
  • 1970-01-01
  • 2017-06-23
相关资源
最近更新 更多