UNPKG

902 BPlain TextView Raw
1import type { GraphQLRequestContext, GraphQLResponse } from '../helpers/types.js'
2
3export class ClientError extends Error {
4 public response: GraphQLResponse
5 public request: GraphQLRequestContext
6
7 constructor(response: GraphQLResponse, request: GraphQLRequestContext) {
8 const message = `${ClientError.extractMessage(response)}: ${
9 JSON.stringify({
10 response,
11 request,
12 })
13 }`
14
15 super(message)
16
17 Object.setPrototypeOf(this, ClientError.prototype)
18
19 this.response = response
20 this.request = request
21
22 // this is needed as Safari doesn't support .captureStackTrace
23 if (typeof Error.captureStackTrace === `function`) {
24 Error.captureStackTrace(this, ClientError)
25 }
26 }
27
28 private static extractMessage(response: GraphQLResponse): string {
29 return response.errors?.[0]?.message ?? `GraphQL Error (Code: ${String(response.status)})`
30 }
31}