ApolloErrorHandler.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. CombinedGraphQLErrors,
  3. CombinedProtocolErrors,
  4. LocalStateError,
  5. ServerError,
  6. ServerParseError,
  7. UnconventionalError,
  8. } from "@apollo/client/errors";
  9. import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
  10. // about apollo client error handle https://www.apollographql.com/docs/react/data/error-handling
  11. // Comprehensive error handling example.
  12. export function handleApolloBusinessError(error: unknown, callback: (err:CombinedGraphQLErrors) => void) {
  13. if (CombinedGraphQLErrors.is(error)) {
  14. // Handle GraphQL errors
  15. // error.errors.forEach(({ message, locations, path }) =>
  16. // console.log(
  17. // `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  18. // )
  19. // );
  20. callback(error);
  21. // console.log('CombinedGraphQLErrors--',error.message);
  22. }
  23. }
  24. // graphql接口额响应状态码貌似只有200 和 500。500表示: 1.后端代码报错;2.前端代理接口抛出异常
  25. // 已知有一种情况graphql的响应状态码是401,即请求接口的时候请求头缺少X-STOREFRONT-KEY
  26. export function handleErrorForErrorLink(error: unknown) {
  27. // CombinedGraphQLErrors 交给具体的业务自己捕获处理
  28. if (CombinedGraphQLErrors.is(error)) return;
  29. if (CombinedProtocolErrors.is(error)) {
  30. // CombinedProtocolErrors 是GraphQL 传输协议层错误,该项目中遇不到
  31. // Handle multipart subscription protocol errors
  32. console.error( "GraphQL protocol error", error.errors);
  33. console.log(error.message);
  34. } else if (LocalStateError.is(error)) {
  35. /**
  36. * LocalStateError 是 Apollo Client 在执行 Local State(本地状态) 时抛出的错误,通常和 @client 字段、本地 resolver 配置、LocalState 配置错误 有关。
  37. * 它不是 GraphQL Server 返回的错误,而是 Apollo Client 自己在本地解析字段时发生的致命错误。
  38. * 本项目没用到
  39. */
  40. // Handle errors thrown by the `LocalState` class
  41. console.error( "Local state error message:", error.message );
  42. console.error( "Local state error cause:", error.cause );
  43. console.error( "Local state error path:", error.path );
  44. } else if (ServerError.is(error)) {
  45. // 状态码不是2xx的错误
  46. // Handle server HTTP errors
  47. /*
  48. console.log('ServerError--',error);
  49. console.log(`Server returned status: ${error.statusCode}`,error.message);
  50. console.log(`bodyText: `,JSON.parse(error.bodyText));
  51. console.log('response: ', error.response);
  52. */
  53. const bodyData = JSON.parse(error.bodyText);
  54. const msg = bodyData.errors[0].message;
  55. // Handle specific status codes
  56. confirmDialog({
  57. title: "Server Error",
  58. content: 'AC Error: ' + msg,
  59. noCancel: true,
  60. }).then(() => {});
  61. } else if (ServerParseError.is(error)) {
  62. // 解析接口返回数据时报错
  63. // Handle JSON parse errors
  64. console.log('ServerParseError--',error);
  65. } else if (UnconventionalError.is(error)) {
  66. // 不是标准的JavaScript错误,一般是自定义apollo link或者第三方Apollo link抛出的
  67. // Handle errors thrown by irregular types
  68. console.log('UnconventionalError--',error);
  69. console.error( "非法错误:", error.cause);
  70. } else {
  71. // Handle other errors
  72. console.error("[other errors]:", error);
  73. }
  74. }