Skip to content
LogoLogo

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:

reasonMeaning
graphQLThe response came back with a top-level errors array.
malformedThe response body wasn't valid GraphQL JSON.
networkThe request couldn't reach the server.
statusThe response status was outside the 200-299 range.
timeoutThe request took longer than the client's configured timeout.
transformThe 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";
PropertyDescription
reasonWhy the request failed, as listed above.
urlThe request URL.
responseThe raw fetch response, when one was received.
graphQLErrorsThe parsed errors array. Only populated when reason is "graphQL".

graphQLErrors holds instances of GraphQLError from @0no-co/graphql.web, a dependency of this package.