我正在使用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
在比较这两种设计的利弊之后,我仍然无法确定哪一种更好。
这种错误处理是否有通用设计? 提前致谢。