UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

76 lines (75 loc) 2.59 kB
import { brand, isBranded } from "./utils.js"; /** * Represents an error when a non-200 HTTP status code is returned from the * server according to the [GraphQL Over HTTP specification](https://graphql.github.io/graphql-over-http/draft/). This error * contains the full server response, including status code and body text. * * @remarks * * 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`](https://graphql.github.io/graphql-over-http/draft/#sec-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. * * @example * * ```ts * import { ServerError } from "@apollo/client/errors"; * * // Check if an error is a ServerError instance * if (ServerError.is(error)) { * console.log(`Server returned status: ${error.statusCode}`); * console.log(`Response body: ${error.bodyText}`); * * // Handle specific status codes * if (error.statusCode === 401) { * // Handle unauthorized access * } * } * ``` */ export class ServerError extends Error { /** * A method that determines whether an error is a `ServerError` object. This * method enables TypeScript to narrow the error type. * * @example * * ```ts * if (ServerError.is(error)) { * // TypeScript now knows `error` is a ServerError object * console.log(error.errors); * } * ``` */ static is(error) { return isBranded(error, "ServerError"); } /** * The raw [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object provided by the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). */ response; /** * The status code returned by the server in the response. This is provided as * a shortcut for `response.status`. */ statusCode; /** * The raw response body text. */ bodyText; constructor(message, options) { super(message); this.name = "ServerError"; this.response = options.response; this.statusCode = options.response.status; this.bodyText = options.bodyText; brand(this); Object.setPrototypeOf(this, ServerError.prototype); } } //# sourceMappingURL=ServerError.js.map