import { CombinedGraphQLErrors, CombinedProtocolErrors, LocalStateError, ServerError, ServerParseError, UnconventionalError, } from "@apollo/client/errors"; import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api"; // about apollo client error handle https://www.apollographql.com/docs/react/data/error-handling // Comprehensive error handling example. export function handleApolloBusinessError(error: unknown, callback: (err:CombinedGraphQLErrors) => void) { if (CombinedGraphQLErrors.is(error)) { // Handle GraphQL errors // error.errors.forEach(({ message, locations, path }) => // console.log( // `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}` // ) // ); callback(error); // console.log('CombinedGraphQLErrors--',error.message); } } // graphql接口额响应状态码貌似只有200 和 500。500表示: 1.后端代码报错;2.前端代理接口抛出异常 // 已知有一种情况graphql的响应状态码是401,即请求接口的时候请求头缺少X-STOREFRONT-KEY export function handleErrorForErrorLink(error: unknown) { // CombinedGraphQLErrors 交给具体的业务自己捕获处理 if (CombinedGraphQLErrors.is(error)) return; if (CombinedProtocolErrors.is(error)) { // CombinedProtocolErrors 是GraphQL 传输协议层错误,该项目中遇不到 // Handle multipart subscription protocol errors console.error( "GraphQL protocol error", error.errors); console.log(error.message); } else if (LocalStateError.is(error)) { /** * LocalStateError 是 Apollo Client 在执行 Local State(本地状态) 时抛出的错误,通常和 @client 字段、本地 resolver 配置、LocalState 配置错误 有关。 * 它不是 GraphQL Server 返回的错误,而是 Apollo Client 自己在本地解析字段时发生的致命错误。 * 本项目没用到 */ // Handle errors thrown by the `LocalState` class console.error( "Local state error message:", error.message ); console.error( "Local state error cause:", error.cause ); console.error( "Local state error path:", error.path ); } else if (ServerError.is(error)) { // 状态码不是2xx的错误 // Handle server HTTP errors /* console.log('ServerError--',error); console.log(`Server returned status: ${error.statusCode}`,error.message); console.log(`bodyText: `,JSON.parse(error.bodyText)); console.log('response: ', error.response); */ const bodyData = JSON.parse(error.bodyText); const msg = bodyData.errors[0].message; // Handle specific status codes confirmDialog({ title: "Server Error", content: 'AC Error: ' + msg, noCancel: true, }).then(() => {}); } else if (ServerParseError.is(error)) { // 解析接口返回数据时报错 // Handle JSON parse errors console.log('ServerParseError--',error); } else if (UnconventionalError.is(error)) { // 不是标准的JavaScript错误,一般是自定义apollo link或者第三方Apollo link抛出的 // Handle errors thrown by irregular types console.log('UnconventionalError--',error); console.error( "非法错误:", error.cause); } else { // Handle other errors console.error("[other errors]:", error); } }