-
Notifications
You must be signed in to change notification settings - Fork 565
/
GraphQLYogaError.ts
45 lines (40 loc) · 1.28 KB
/
GraphQLYogaError.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
import { EnvelopError } from '@envelop/core'
import { createGraphQLError } from '@graphql-tools/utils'
import { GraphQLError } from 'graphql'
declare module 'graphql' {
interface GraphQLHTTPErrorExtensions {
status?: number
headers?: Record<string, string>
}
interface GraphQLErrorExtensions {
http?: GraphQLHTTPErrorExtensions
}
}
export { EnvelopError as GraphQLYogaError }
function isAggregateError(obj: any): obj is AggregateError {
return obj != null && typeof obj === 'object' && 'errors' in obj
}
function hasToString(obj: any): obj is { toString(): string } {
return obj != null && typeof obj.toString === 'function'
}
export function handleError(
error: unknown,
errors: GraphQLError[] = [],
): GraphQLError[] {
if (isAggregateError(error)) {
for (const singleError of error.errors) {
errors.push(...handleError(singleError))
}
} else if (error instanceof GraphQLError) {
errors.push(error)
} else if (error instanceof Error) {
errors.push(createGraphQLError(error.message))
} else if (typeof error === 'string') {
errors.push(createGraphQLError(error))
} else if (hasToString(error)) {
errors.push(createGraphQLError(error.toString()))
} else {
errors.push(createGraphQLError('Unexpected error!'))
}
return errors
}