-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
errors.ts
133 lines (116 loc) · 3.52 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
GraphQLResolveInfo,
responsePathAsArray,
ExecutionResult,
GraphQLError,
} from 'graphql';
import { locatedError } from 'graphql/error';
import { getResponseKeyFromInfo } from './getResponseKeyFromInfo';
export let ERROR_SYMBOL: any;
if (
(typeof global !== 'undefined' && 'Symbol' in global) ||
(typeof window !== 'undefined' && 'Symbol' in window)
) {
ERROR_SYMBOL = Symbol('subSchemaErrors');
} else {
ERROR_SYMBOL = '@@__subSchemaErrors';
}
export function annotateWithChildrenErrors(object: any, childrenErrors: ReadonlyArray<GraphQLError>): any {
if (!childrenErrors || childrenErrors.length === 0) {
// Nothing to see here, move along
return object;
}
if (Array.isArray(object)) {
const byIndex = {};
childrenErrors.forEach(error => {
if (!error.path) {
return;
}
const index = error.path[1];
const current = byIndex[index] || [];
// It's important to keep the `originalError`, to make sure
// error stacktrace's and custom `extensions` are preserved from
// source schemas, when merged.
current.push({
...error,
path: error.path.slice(1),
originalError: error.originalError,
});
byIndex[index] = current;
});
return object.map((item, index) => annotateWithChildrenErrors(item, byIndex[index]));
}
return {
...object,
[ERROR_SYMBOL]: childrenErrors.map(error => ({
...error,
...(error.path ? { path: error.path.slice(1) } : {})
}))
};
}
export function getErrorsFromParent(
object: any,
fieldName: string
):
| {
kind: 'OWN';
error: any;
}
| {
kind: 'CHILDREN';
errors?: Array<GraphQLError>;
} {
const errors = (object && object[ERROR_SYMBOL]) || [];
const childrenErrors: Array<GraphQLError> = [];
for (const error of errors) {
if (!error.path || (error.path.length === 1 && error.path[0] === fieldName)) {
return {
kind: 'OWN',
error
};
} else if (error.path[0] === fieldName) {
childrenErrors.push(error);
}
}
return {
kind: 'CHILDREN',
errors: childrenErrors
};
}
class CombinedError extends Error {
public errors: ReadonlyArray<GraphQLError>;
constructor(message: string, errors: ReadonlyArray<GraphQLError>) {
super(message);
this.errors = errors;
}
}
export function checkResultAndHandleErrors(
result: ExecutionResult,
info: GraphQLResolveInfo,
responseKey?: string
): any {
if (!responseKey) {
responseKey = getResponseKeyFromInfo(info);
}
if (result.errors && (!result.data || result.data[responseKey] == null)) {
// apollo-link-http & http-link-dataloader need the
// result property to be passed through for better error handling.
// If there is only one error, which contains a result property, pass the error through
const newError =
result.errors.length === 1 && hasResult(result.errors[0])
? result.errors[0]
: new CombinedError(concatErrors(result.errors), result.errors);
throw locatedError(newError, info.fieldNodes, responsePathAsArray(info.path));
}
let resultObject = result.data[responseKey];
if (result.errors) {
resultObject = annotateWithChildrenErrors(resultObject, result.errors as ReadonlyArray<GraphQLError>);
}
return resultObject;
}
function concatErrors(errors: ReadonlyArray<GraphQLError>) {
return errors.map(error => error.message).join('\n');
}
function hasResult(error: any) {
return error.result || error.extensions || (error.originalError && error.originalError.result);
}