@stephenchenorg/astro 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/error.d.ts +33 -10
- package/dist/api/error.js +24 -1
- package/dist/api/errorResponse.d.ts +1 -0
- package/dist/api/errorResponse.js +9 -0
- package/dist/api/fetch.js +14 -4
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/package.json +1 -1
package/dist/api/error.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
interface
|
|
1
|
+
export interface GraphQLFieldError {
|
|
2
|
+
code?: number;
|
|
3
|
+
message: string;
|
|
4
|
+
locations?: {
|
|
5
|
+
line: number;
|
|
6
|
+
column: number;
|
|
7
|
+
}[];
|
|
8
|
+
path?: (string | number)[];
|
|
9
|
+
extensions?: GraphQLRequestExtensions;
|
|
10
|
+
data?: Record<string, any>;
|
|
11
|
+
}
|
|
12
|
+
export interface GraphQLRequestExtensions {
|
|
2
13
|
debugMessage?: string;
|
|
3
14
|
file?: string;
|
|
4
15
|
line?: number;
|
|
@@ -7,25 +18,37 @@ interface GraphQLRequestExtensions {
|
|
|
7
18
|
line: number;
|
|
8
19
|
call: number;
|
|
9
20
|
}[];
|
|
10
|
-
|
|
11
|
-
|
|
21
|
+
}
|
|
22
|
+
export interface GraphQLRequestErrorOptions {
|
|
23
|
+
code?: number;
|
|
24
|
+
message: string;
|
|
25
|
+
query: string;
|
|
26
|
+
variables?: Record<string, any>;
|
|
27
|
+
extensions?: GraphQLRequestExtensions;
|
|
28
|
+
fieldErrors?: GraphQLFieldError[];
|
|
12
29
|
}
|
|
13
30
|
export declare class GraphQLRequestError extends Error {
|
|
14
31
|
type: string;
|
|
15
32
|
name: string;
|
|
16
33
|
title: string;
|
|
34
|
+
code: number | undefined;
|
|
17
35
|
originalMessage: string;
|
|
18
36
|
query: string;
|
|
19
37
|
variables: Record<string, any> | undefined;
|
|
20
38
|
extensions: GraphQLRequestExtensions | undefined;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
query: string;
|
|
24
|
-
variables?: Record<string, any>;
|
|
25
|
-
extensions?: GraphQLRequestExtensions;
|
|
26
|
-
}, options?: ErrorOptions);
|
|
39
|
+
fieldErrors: GraphQLFieldError[] | undefined;
|
|
40
|
+
constructor(props: GraphQLRequestErrorOptions, options?: ErrorOptions);
|
|
27
41
|
isNotFound(): boolean;
|
|
28
42
|
private buildMessage;
|
|
29
43
|
static is(err: unknown): err is GraphQLRequestError;
|
|
30
44
|
}
|
|
31
|
-
export {
|
|
45
|
+
export declare class GraphQLNotFoundError extends GraphQLRequestError {
|
|
46
|
+
type: string;
|
|
47
|
+
name: string;
|
|
48
|
+
}
|
|
49
|
+
export declare class GraphQLValidationError extends GraphQLRequestError {
|
|
50
|
+
type: string;
|
|
51
|
+
name: string;
|
|
52
|
+
errors: Record<string, string[]>;
|
|
53
|
+
constructor(props: GraphQLRequestErrorOptions, options?: ErrorOptions);
|
|
54
|
+
}
|
package/dist/api/error.js
CHANGED
|
@@ -2,19 +2,23 @@ export class GraphQLRequestError extends Error {
|
|
|
2
2
|
type = "GraphQLRequestError";
|
|
3
3
|
name = "GraphQLRequestError";
|
|
4
4
|
title = "GraphQL request error.";
|
|
5
|
+
code;
|
|
5
6
|
originalMessage;
|
|
6
7
|
query;
|
|
7
8
|
variables;
|
|
8
9
|
extensions;
|
|
10
|
+
fieldErrors;
|
|
9
11
|
constructor(props, options) {
|
|
10
|
-
const { message, query, variables, extensions } = props;
|
|
12
|
+
const { code, message, query, variables, extensions, fieldErrors } = props;
|
|
11
13
|
super("GraphQL request error.", options);
|
|
12
14
|
const originalStack = this.stack;
|
|
13
15
|
this.title = "GraphQL request error.";
|
|
16
|
+
this.code = code;
|
|
14
17
|
this.originalMessage = message;
|
|
15
18
|
this.query = query;
|
|
16
19
|
this.variables = variables;
|
|
17
20
|
this.extensions = extensions;
|
|
21
|
+
this.fieldErrors = fieldErrors;
|
|
18
22
|
this.message = this.buildMessage();
|
|
19
23
|
this.stack = originalStack;
|
|
20
24
|
}
|
|
@@ -24,6 +28,12 @@ export class GraphQLRequestError extends Error {
|
|
|
24
28
|
buildMessage() {
|
|
25
29
|
let message = `${this.originalMessage}
|
|
26
30
|
`;
|
|
31
|
+
if (this.code) {
|
|
32
|
+
message += "\n";
|
|
33
|
+
message += `[code]
|
|
34
|
+
${this.code}
|
|
35
|
+
`;
|
|
36
|
+
}
|
|
27
37
|
if (this.extensions) {
|
|
28
38
|
if (this.extensions.debugMessage) {
|
|
29
39
|
message += "\n";
|
|
@@ -72,3 +82,16 @@ ${JSON.stringify(this.variables, null, 2)}
|
|
|
72
82
|
return err.type === "GraphQLRequestError";
|
|
73
83
|
}
|
|
74
84
|
}
|
|
85
|
+
export class GraphQLNotFoundError extends GraphQLRequestError {
|
|
86
|
+
type = "GraphQLNotFoundError";
|
|
87
|
+
name = "GraphQLNotFoundError";
|
|
88
|
+
}
|
|
89
|
+
export class GraphQLValidationError extends GraphQLRequestError {
|
|
90
|
+
type = "GraphQLValidationError";
|
|
91
|
+
name = "GraphQLValidationError";
|
|
92
|
+
errors;
|
|
93
|
+
constructor(props, options) {
|
|
94
|
+
super(props, options);
|
|
95
|
+
this.errors = this.fieldErrors?.[0]?.data?.validation || {};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function handleErrorResponse(e: unknown): Response | undefined;
|
package/dist/api/fetch.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AwesomeGraphQLClient, GraphQLRequestError as AwesomeGraphQLRequestError } from "awesome-graphql-client";
|
|
2
2
|
import { print } from "graphql/language/printer";
|
|
3
|
-
import { GraphQLRequestError } from "./error.js";
|
|
3
|
+
import { GraphQLNotFoundError, GraphQLRequestError, GraphQLValidationError } from "./error.js";
|
|
4
4
|
export function createGraphQLAPI(options) {
|
|
5
5
|
return function graphQLAPI(query, variables = {}) {
|
|
6
6
|
const client = new AwesomeGraphQLClient({
|
|
@@ -17,12 +17,22 @@ export function createGraphQLAPI(options) {
|
|
|
17
17
|
...variables
|
|
18
18
|
}, fetchOptions).then((data) => resolve(data)).catch((error) => {
|
|
19
19
|
if (error instanceof AwesomeGraphQLRequestError) {
|
|
20
|
-
|
|
20
|
+
const fieldError = error.fieldErrors?.[0];
|
|
21
|
+
const code = fieldError?.code;
|
|
22
|
+
const errorProps = {
|
|
23
|
+
code,
|
|
21
24
|
message: error.message,
|
|
22
25
|
query: error.query,
|
|
23
26
|
variables: error.variables,
|
|
24
|
-
extensions: error.extensions
|
|
25
|
-
|
|
27
|
+
extensions: error.extensions,
|
|
28
|
+
fieldErrors: error.fieldErrors
|
|
29
|
+
};
|
|
30
|
+
if (code === 404) {
|
|
31
|
+
reject(new GraphQLNotFoundError(errorProps));
|
|
32
|
+
} else if (code === 422) {
|
|
33
|
+
reject(new GraphQLValidationError(errorProps));
|
|
34
|
+
}
|
|
35
|
+
reject(new GraphQLRequestError(errorProps));
|
|
26
36
|
} else {
|
|
27
37
|
reject(error);
|
|
28
38
|
}
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.js
CHANGED