-
-
Notifications
You must be signed in to change notification settings - Fork 816
/
errors.ts
75 lines (70 loc) · 1.81 KB
/
errors.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
import { ASTNode, GraphQLError, Source, versionInfo } from 'graphql';
import { Maybe } from './types.js';
interface GraphQLErrorOptions {
nodes?: ReadonlyArray<ASTNode> | ASTNode | null;
source?: Maybe<Source>;
positions?: Maybe<ReadonlyArray<number>>;
path?: Maybe<ReadonlyArray<string | number>>;
originalError?: Maybe<
Error & {
readonly extensions?: unknown;
}
>;
extensions?: any;
}
const possibleGraphQLErrorProperties = [
'message',
'locations',
'path',
'nodes',
'source',
'positions',
'originalError',
'name',
'stack',
'extensions',
];
function isGraphQLErrorLike(error: any) {
return (
error != null &&
typeof error === 'object' &&
Object.keys(error).every(key => possibleGraphQLErrorProperties.includes(key))
);
}
export function createGraphQLError(message: string, options?: GraphQLErrorOptions): GraphQLError {
if (
options?.originalError &&
!(options.originalError instanceof Error) &&
isGraphQLErrorLike(options.originalError)
) {
options.originalError = createGraphQLError(
(options.originalError as any).message,
options.originalError,
);
}
if (versionInfo.major >= 17) {
return new (GraphQLError as any)(message, options);
}
return new (GraphQLError as any)(
message,
options?.nodes,
options?.source,
options?.positions,
options?.path,
options?.originalError,
options?.extensions,
);
}
export function relocatedError(
originalError: GraphQLError,
path?: ReadonlyArray<string | number>,
): GraphQLError {
return createGraphQLError(originalError.message, {
nodes: originalError.nodes,
source: originalError.source,
positions: originalError.positions,
path: path == null ? originalError.path : path,
originalError,
extensions: originalError.extensions,
});
}