UNPKG

@apollo/client

Version:

A fully-featured caching GraphQL client.

1 lines 5.88 kB
{"version":3,"file":"ServerError.cjs","sources":["../../../src/errors/ServerError.ts"],"sourcesContent":["import { brand, isBranded } from \"./utils.js\";\n\nexport declare namespace ServerError {\n export interface Options {\n response: Response;\n bodyText: string;\n }\n\n namespace DocumentationTypes {\n interface InstanceProperties {\n /**\n * 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).\n */\n readonly response: Response;\n /**\n * The status code returned by the server in the response. This is provided as\n * a shortcut for `response.status`.\n */\n readonly statusCode: number;\n\n /**\n * The raw response body text.\n */\n readonly bodyText: string;\n }\n }\n}\n\n/**\n * Represents an error when a non-200 HTTP status code is returned from the\n * server according to the [GraphQL Over HTTP specification](https://graphql.github.io/graphql-over-http/draft/). This error\n * contains the full server response, including status code and body text.\n *\n * @remarks\n *\n * This error occurs when your GraphQL server responds with an HTTP status code\n * other than 200 (such as 4xx or 5xx status codes) with any media type other\n * than [`application/graphql-response+json`](https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json).\n *\n * Servers that return non-200 status codes with other media types are not\n * guaranteed to contain a well-formed GraphQL response and may indicate issues\n * at the HTTP level, such as authentication failures, server unavailability,\n * or other HTTP-level problems.\n *\n * @example\n *\n * ```ts\n * import { ServerError } from \"@apollo/client/errors\";\n *\n * // Check if an error is a ServerError instance\n * if (ServerError.is(error)) {\n * console.log(`Server returned status: ${error.statusCode}`);\n * console.log(`Response body: ${error.bodyText}`);\n *\n * // Handle specific status codes\n * if (error.statusCode === 401) {\n * // Handle unauthorized access\n * }\n * }\n * ```\n */\nexport class ServerError extends Error {\n /**\n * A method that determines whether an error is a `ServerError` object. This\n * method enables TypeScript to narrow the error type.\n *\n * @example\n *\n * ```ts\n * if (ServerError.is(error)) {\n * // TypeScript now knows `error` is a ServerError object\n * console.log(error.errors);\n * }\n * ```\n */\n static is(error: unknown): error is ServerError {\n return isBranded(error, \"ServerError\");\n }\n\n /** {@inheritDoc @apollo/client!ServerError.DocumentationTypes.InstanceProperties#response:member} */\n readonly response: Response;\n\n /** {@inheritDoc @apollo/client!ServerError.DocumentationTypes.InstanceProperties#statusCode:member} */\n readonly statusCode: number;\n\n /** {@inheritDoc @apollo/client!ServerError.DocumentationTypes.InstanceProperties#bodyText:member} */\n readonly bodyText: string;\n\n constructor(message: string, options: ServerError.Options) {\n super(message);\n this.name = \"ServerError\";\n this.response = options.response;\n this.statusCode = options.response.status;\n this.bodyText = options.bodyText;\n\n brand(this);\n Object.setPrototypeOf(this, ServerError.prototype);\n }\n}\n"],"names":[],"mappings":";;;AAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;AA4BA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCA,CAAA;AACA,CAAA,CAAA,CAAA,CAAA,EAAa,CAAb,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAiC,CAAjC,CAAA,CAAA,CAAA,EAAA;IACE,CAAF,CAAA;;;;;;;;;;;;KAYA,CAAA;IACE,CAAF,CAAA,CAAA,CAAA,CAAA,EAAS,CAAT,CAAW,CAAC,CAAZ,CAAA,CAAA,CAAA,CAA0B,EAA1B;QACI,CAAJ,CAAA,CAAA,CAAA,CAAA,EAAW,CAAX,CAAA,EAAW,CAAX,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAoB,CAApB,CA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