Errors
The ClientError thrown by queries and mutations.
Every failed request throws a ClientError, never a raw fetch or parsing exception. Its reason field tells you what went wrong:
reason | Meaning |
|---|---|
graphQL | The response came back with a top-level errors array. |
malformed | The response body wasn't valid GraphQL JSON. |
network | The request couldn't reach the server. |
status | The response status was outside the 200-299 range. |
timeout | The request took longer than the client's configured timeout. |
transform | The client's transformRequest function threw or rejected. |
In useQuery, the error is thrown during render, to be caught by an ErrorBoundary. In useDeferredQuery and useMutation, it lands in state.error (and rejects the returned promise).
Usage
import { ClientError } from "@zoontek/gql-client";
try {
await client.mutate(mutation, variables);
} catch (error) {
if (error instanceof ClientError && error.reason === "graphQL") {
console.log(error.graphQLErrors);
}
}Shape
class ClientError extends Error {
reason: ClientErrorReason;
url: string;
response: Response | undefined;
graphQLErrors: GraphQLError[];
}
type ClientErrorReason =
"graphQL" | "malformed" | "network" | "status" | "timeout" | "transform";| Property | Description |
|---|---|
reason | Why the request failed, as listed above. |
url | The request URL. |
response | The raw fetch response, when one was received. |
graphQLErrors | The parsed errors array. Only populated when reason is "graphQL". |
graphQLErrors holds instances of GraphQLError from @0no-co/graphql.web, a dependency of this package.