[英]Apollo GraphQL Client + Next.JS Error Handling
The component renders the error state just fine, but the exception is displayed as uncaught in the console and a dialogue is displayed in next dev on the browser.该组件很好地呈现错误状态,但异常在控制台中显示为未捕获,并且在浏览器的下一个开发中显示对话框。 Is there a way to handle expected errors to squelch this behavior?
有没有办法处理预期的错误来抑制这种行为?
import { useMutation, gql } from "@apollo/client";
import { useEffect } from "react";
const CONSUME_MAGIC_LINK = gql`
mutation ConsumeMagicLink($token: String!) {
consumeMagicLink(token: $token) {
token
member {
id
}
}
}
`;
export default function ConsumeMagicLink({ token }) {
const [consumeMagicLink, { data, loading, error }] =
useMutation(CONSUME_MAGIC_LINK);
console.log("DATA", data, "loading:", loading, "error:", error);
useEffect(() => {
try {
consumeMagicLink({ variables: { token } });
} catch (e) {
console.log(e);
}
}, []);
var text = "Link has expired or has been used previously";
if (data) text = "SUCCESS: REDIRECTING";
if (loading) text = "Processing";
if (error) text = "Link has expired or has been used previously";
return (
<div>
<h2>{text}</h2>
</div>
);
}
Console Results:控制台结果:
Error Displayed in Browser:浏览器中显示的错误:
The error is from the client instead of the mutation so your try-catch cannot catch it.错误来自客户端而不是突变,因此您的 try-catch 无法捕获它。 To handle this you can add the error handling to the client, for example:
要处理此问题,您可以将错误处理添加到客户端,例如:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
});
const httpLink = new HttpLink({
uri: "some invalid link"
});
const client = new ApolloClient({
link:from([httpLink,errorLink]),
cache: new InMemoryCache()
})
And as you got authorization error, I suggest you check your headers.当您遇到授权错误时,我建议您检查您的标题。
With nextjs, how to prevent modal show up every occur error in Apollo Link?使用 nextjs,如何防止 modal 显示 Apollo Link 中出现的每一个错误? Anyone help me?
有人帮我吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.