-
-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy patherror.ts
120 lines (110 loc) · 3.7 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { GraphQLError } from '@0no-co/graphql.web';
import type { ErrorLike } from '../types';
const generateErrorMessage = (
networkErr?: Error,
graphQlErrs?: GraphQLError[]
) => {
let error = '';
if (networkErr) return `[Network] ${networkErr.message}`;
if (graphQlErrs) {
for (const err of graphQlErrs) {
if (error) error += '\n';
error += `[GraphQL] ${err.message}`;
}
}
return error;
};
const rehydrateGraphQlError = (error: any): GraphQLError => {
if (
error &&
error.message &&
(error.extensions || error.name === 'GraphQLError')
) {
return error;
} else if (typeof error === 'object' && error.message) {
return new GraphQLError(
error.message,
error.nodes,
error.source,
error.positions,
error.path,
error,
error.extensions || {}
);
} else {
return new GraphQLError(error as any);
}
};
/** An abstracted `Error` that provides either a `networkError` or `graphQLErrors`.
*
* @remarks
* During a GraphQL request, either the request can fail entirely, causing a network error,
* or the GraphQL execution or fields can fail, which will cause an {@link ExecutionResult}
* to contain an array of GraphQL errors.
*
* The `CombinedError` abstracts and normalizes both failure cases. When {@link OperationResult.error}
* is set to this error, the `CombinedError` abstracts all errors, making it easier to handle only
* a subset of error cases.
*
* @see {@link https://urql.dev/goto/docs/basics/errors} for more information on handling
* GraphQL errors and the `CombinedError`.
*/
export class CombinedError extends Error {
public name: string;
public message: string;
/** A list of GraphQL errors rehydrated from a {@link ExecutionResult}.
*
* @remarks
* If an {@link ExecutionResult} received from the API contains a list of errors,
* the `CombinedError` will rehydrate them, normalize them to
* {@link GraphQLError | GraphQLErrors} and list them here.
* An empty list indicates that no GraphQL error has been sent by the API.
*/
public graphQLErrors: GraphQLError[];
/** Set to an error, if a GraphQL request has failed outright.
*
* @remarks
* A GraphQL over HTTP request may fail and not reach the API. Any error that
* prevents a GraphQl request outright, will be considered a “network error” and
* set here.
*/
public networkError?: Error;
/** Set to the {@link Response} object a fetch exchange received.
*
* @remarks
* If a built-in fetch {@link Exchange} is used in `urql`, this may
* be set to the {@link Response} object of the Fetch API response.
* However, since `urql` doesn’t assume that all users will use HTTP
* as the only or exclusive transport for GraphQL this property is
* neither typed nor guaranteed and may be re-used for other purposes
* by non-fetch exchanges.
*
* Hint: It can be useful to use `response.status` here, however, if
* you plan on relying on this being a {@link Response} in your app,
* which it is by default, then make sure you add some extra checks
* before blindly assuming so!
*/
public response?: any;
constructor(input: {
networkError?: Error;
graphQLErrors?: Array<string | ErrorLike>;
response?: any;
}) {
const normalizedGraphQLErrors = (input.graphQLErrors || []).map(
rehydrateGraphQlError
);
const message = generateErrorMessage(
input.networkError,
normalizedGraphQLErrors
);
super(message);
this.name = 'CombinedError';
this.message = message;
this.graphQLErrors = normalizedGraphQLErrors;
this.networkError = input.networkError;
this.response = input.response;
}
toString(): string {
return this.message;
}
}