-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy patherror.ts
38 lines (31 loc) · 1.15 KB
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { ResponseHeaders } from "@octokit/types";
import type { GraphQlEndpointOptions, GraphQlQueryResponse } from "./types.js";
type ServerResponseData<T> = Required<GraphQlQueryResponse<T>>;
function _buildMessageForResponseErrors(
data: ServerResponseData<unknown>,
): string {
return (
`Request failed due to following response errors:\n` +
data.errors.map((e) => ` - ${e.message}`).join("\n")
);
}
export class GraphqlResponseError<ResponseData> extends Error {
override name = "GraphqlResponseError";
readonly errors: GraphQlQueryResponse<never>["errors"];
readonly data: ResponseData;
constructor(
readonly request: GraphQlEndpointOptions,
readonly headers: ResponseHeaders,
readonly response: ServerResponseData<ResponseData>,
) {
super(_buildMessageForResponseErrors(response));
// Expose the errors and response data in their shorthand properties.
this.errors = response.errors;
this.data = response.data;
// Maintains proper stack trace (only available on V8)
/* istanbul ignore next */
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
}