ServerError
API reference
Represents an error when a non-200 HTTP status code is returned from the server according to the GraphQL Over HTTP specification. This error contains the full server response, including status code and body text.
This error occurs when your GraphQL server responds with an HTTP status code
other than 200 (such as 4xx or 5xx status codes) with any media type other
than application/graphql-response+json.
Servers that return non-200 status codes with other media types are not guaranteed to contain a well-formed GraphQL response and may indicate issues at the HTTP level, such as authentication failures, server unavailability, or other HTTP-level problems.
1 import { ServerError } from "@apollo/client/errors";
2
3 // Check if an error is a ServerError instance
4 if (ServerError.is(error)) {
5 console.log(`Server returned status: ${error.statusCode}`);
6 console.log(`Response body: ${error.bodyText}`);
7
8 // Handle specific status codes
9 if (error.statusCode === 401) {
10 // Handle unauthorized access
11 }
12 }Static methods
A method that determines whether an error is a ServerError object. This
method enables TypeScript to narrow the error type.
Example
1 if (ServerError.is(error)) {
2 // TypeScript now knows `error` is a ServerError object
3 console.log(error.errors);
4 }Signature
1is(
2 error: unknown
3): error is ServerErrorSee the instance properties for more details about the available properties provided by the ServerError object.
Instance properties
These properties are specific to the ServerError object. Standard error instance properties are also available.
stringThe raw response body text.
ResponsenumberThe status code returned by the server in the response. This is provided as
a shortcut for response.status.