在我的带有节点服务器的 React ApolloGraphQL 应用程序中,我试图让它在我的节点服务器关闭时捕获错误。当节点服务器我们时,一切正常......捕获并显示图形 QL 错误。 但是当我停止节点服务器时,我收到一个无法避免的运行时错误。当节点服务器关闭时,服务器会以503 Service Unavailable
响应和不是 JSON 的 html 页面进行响应。
阿波罗客户端的设置如下:
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)
从 useMutation 钩子返回的error
数据:
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 yournrequest due to maintenance downtime or capacitynproblems. Please try again later.
</p>n<p>Additionally, a 503 Service Unavailablenerror 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(() => {...});