Apollo在应用中的一点错误处理

Apollo error handling in one point in application

我想在我的 apollo react 应用程序中添加错误处理。我正在检查错误

export enum ERROR_CODES {
  ERROR_USER_NOT_EXIST = "ERROR_USER_NOT_EXIST"
}

export const getErrorMessage = (error: string): string | null => {
  switch(error) {
    case ERROR_CODES.ERROR_USER_NOT_EXIST:
      return 'test error';
    default:
      return null;
  }
}

我想显示 snackBar 我在 switch case 中遇到的错误。

我知道我不能用 apollo-link-error 来做,因为我想显示像反应组件这样的错误,我不想为我的每个查询请求添加错误处理组件。也许存在在我的应用程序中的一点上完成它并且没有 apollo-link-error.

的方法

使用apollo-link-error;)

没有人强迫您只 console.log() 出错或中断数据流。您只能将它用于 "inject your hook",以便收到错误通知。

您可以将您的应用包装在某个上下文提供程序中 - 创建一个 'communication channel' - 使用它来写入错误(来自错误 link)并在 snackBar 中呈现(读取和清除)它们。