LinkError
API reference
A facade error type that keeps a registry of errors emitted from the link chain. LinkError
is not an error class itself but rather a utility to detect whether an error originated from the link chain.
Use LinkError
to distinguish between errors from the link chain and custom errors. This is helpful for retrying an operation at the call site only when the error originates from the link chain.
The following example creates a custom wrapped query function that detects whether the query includes an operation name and throws if not.
1import { LinkError } from "@apollo/client/errors"; 2 3async function runQuery<TData>(query: TypedDocumentNode<TData>) { 4 if (!hasOperationName(query)) { 5 throw new Error("Queries should have operation names."); 6 } 7 8 return client.watchQuery({ query }); 9} 10 11try { 12 const result = await runQuery(query); 13} catch (error) { 14 // Only log the error if the error wasn't our own custom thrown error 15 if (LinkError.is(error)) { 16 console.log("Got network error:", error.message); 17 } 18}
Here LinkError
detects whether the caught error
originated from the link chain or is the custom error thrown by runQuery
. This avoids the need for more complex code, such as a custom error class, to distinguish between error types.
Static methods
is
A function that determines whether an error originated from the link chain. is
does not provide any type narrowing.
1if (LinkError.is(error)) { 2 // The error originated from the link chain 3 console.log("Got network error:", error.message); 4}