与Graphql互动,错误处理的常见设计是什么

时间:2019-02-13 20:11:33

标签: reactjs error-handling graphql apollo

我正在使用graphql开发React应用程序。

现在,所有API错误均在每个grapqhl调用中处理。最近,我了解到apollo-link-error包可以帮助处理onError函数中的错误,以便可以“集中”处理所有API错误。 比较这两种设计的利弊后,我仍然无法确定哪一种更好。 当前设计:

Client.mutate({
  mutation: editMutationQuery,
  variables: variables,
  fetchPolicy: 'no-cache'
})
  .then( res => {
    //if success
  })
  .catch(e => {
    //if fail open modal to show error
    this.props.openErrorModal(GQLHelper.formatError(e))
  })

onError控制错误:

const linkError = onError(({operation,grahqlError,networkError,forward}) => {
  if (networkError or graphqlError) {
      Modal.error({
          title:'Error',
          showHeaderCloseButton:false,
          footer:footer
      })
  }
});//all errors are handled here 

在比较这两种设计的利弊之后,我仍然无法确定哪一种更好。

这种错误处理是否有通用设计? 提前致谢。

0 个答案:

没有答案