gpt4 book ai didi

reactjs - 当服务器关闭时 React-Apollo 捕获

转载 作者:行者123 更新时间:2023-12-04 13:36:03 25 4
gpt4 key购买 nike

在我的带有节点服务器的 React ApolloGraphQL 应用程序中,当我的节点服务器关闭时,我试图让它捕获错误。当节点服务器启动时,一切正常且正常……捕获并显示 graphQL 错误。但是当我停止节点服务器时,我收到了一个无法避免的运行时错误。当节点服务器关闭时,服务器响应 503 Service Unavailable响应和非 JSON 的 html 页面。

apollo 客户端设置如下:

const client = new ApolloClient({
assumeImmutableResults: true,
fetchOptions: {
credentials: "include"
},
fetch: fetch,
onError: ({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,),);
if (networkError) {console.log(`[Network error]: ${networkError}`);}
response = {errors:null};
},
cache: new InMemoryCache({
freezeResults: true,
dataIdFromObject: obj => {
let dataId = null;
switch (obj.__typename) {
default:
dataId = defaultDataIdFromObject(obj);
}
return dataId;
},
cacheRedirects: {
Query: {
productVariant: (_, args, { getCacheKey }) => {
const cacheKey = getCacheKey({ __typename: "ProductVariant", ...args });
return cacheKey;
}
}
},
}),
});

以上 onError使用此消息阻止响应:
Error: Network error: JSON.parse: unexpected character at line 1 column 1 of the JSON data

我以这种方式设置了突变:
const [mutateVariant, {data, error, loading}] = useMutation(UPDATE_PRODUCT_META);

我这样称呼它:
mutateVariant({ variables: {inputM: {...buildMetaInput(editedData)} }, 
errorPolicy: 'ignore', onError: (error) => Logger("Error:", error)});

你可以看到我用不同的方式尝试过 errorPolicy设置并添加 onError打回来。

但是我不断收到未处理的运行时错误,并且似乎无法捕获和处理它们:
Unhandled Runtime Error

Error: Network error: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Call Stack
ApolloError webpack-internal:///./node_modules/apollo-client/bundle.esm.js (76:28)
error webpack-internal:///./node_modules/apollo-client/bundle.esm.js (1041:48)
notifySubscription webpack-internal:///./node_modules/zen-observable/lib/Observable.js (140:18)
onNotify webpack-internal:///./node_modules/zen-observable/lib/Observable.js (179:21)
error webpack-internal:///./node_modules/zen-observable/lib/Observable.js (240:15)
error/< webpack-internal:///./node_modules/apollo-client/bundle.esm.js (880:76)
error webpack-internal:///./node_modules/apollo-client/bundle.esm.js (880:27)
notifySubscription webpack-internal:///./node_modules/zen-observable/lib/Observable.js (140:18)
onNotify webpack-internal:///./node_modules/zen-observable/lib/Observable.js (179:21)
error webpack-internal:///./node_modules/zen-observable/lib/Observable.js (240:15)
error webpack-internal:///./node_modules/apollo-link-error/lib/bundle.esm.js (53:34)
notifySubscription webpack-internal:///./node_modules/zen-observable/lib/Observable.js (140:18)
onNotify webpack-internal:///./node_modules/zen-observable/lib/Observable.js (179:21)
error webpack-internal:///./node_modules/zen-observable/lib/Observable.js (240:15)
createHttpLink/</</< webpack-internal:///./node_modules/apollo-link-http/lib/bundle.esm.js (92:26)

返回的 error来自 useMutation 钩子(Hook)的数据:
graphQLErrors: Array []
message: "Network error: JSON.parse: unexpected character at line 1 column 1 of the JSON data"
​networkError: {…}
​ ​bodyText: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>503 Service
Unavailable</title>\n</head><body>\n<h1>Service Unavailable</h1>\n<p>The server is temporarily unable
to service your\nrequest due to maintenance downtime or capacity\nproblems. Please try again later.
</p>\n<p>Additionally, a 503 Service Unavailable\nerror was encountered while trying to use an
ErrorDocument to handle the request.</p>\n</body></html>\n"
name: "ServerParseError"
​​ response: Object { }
​​ statusCode: 503

我如何捕捉这个错误?

最佳答案

mutateVariant 函数返回一个 promise 。尝试在调用后添加 .catch()。

mutateVariant().catch(() => {...});

关于reactjs - 当服务器关闭时 React-Apollo 捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62051000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com