1+ import devAssert from '../jsutils/devAssert' ;
12import isObjectLike from '../jsutils/isObjectLike' ;
23import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols' ;
34
@@ -7,9 +8,6 @@ import type { SourceLocation } from '../language/location';
78import { getLocation } from '../language/location' ;
89import { printLocation , printSourceLocation } from '../language/printLocation' ;
910
10- import { formatError } from './formatError' ;
11- import type { GraphQLFormattedError } from './formatError' ;
12-
1311/**
1412 * A GraphQLError describes an Error found during the parse, validate, or
1513 * execute phases of performing a GraphQL operation. In addition to a message
@@ -198,3 +196,48 @@ export function printError(error: GraphQLError): string {
198196
199197 return output ;
200198}
199+
200+ /**
201+ * Given a GraphQLError, format it according to the rules described by the
202+ * Response Format, Errors section of the GraphQL Specification.
203+ */
204+ export function formatError ( error : GraphQLError ) : GraphQLFormattedError {
205+ devAssert ( error , 'Received null or undefined error.' ) ;
206+ const message = error . message ?? 'An unknown error occurred.' ;
207+ const locations = error . locations ;
208+ const path = error . path ;
209+ const extensions = error . extensions ;
210+
211+ return extensions && Object . keys ( extensions ) . length > 0
212+ ? { message, locations, path, extensions }
213+ : { message, locations, path } ;
214+ }
215+
216+ /**
217+ * @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
218+ */
219+ export type GraphQLFormattedError = { |
220+ /**
221+ * A short, human-readable summary of the problem that **SHOULD NOT** change
222+ * from occurrence to occurrence of the problem, except for purposes of
223+ * localization.
224+ */
225+ + message : string ,
226+ /**
227+ * If an error can be associated to a particular point in the requested
228+ * GraphQL document, it should contain a list of locations.
229+ */
230+ + locations : $ReadOnlyArray < SourceLocation > | void ,
231+ /**
232+ * If an error can be associated to a particular field in the GraphQL result,
233+ * it _must_ contain an entry with the key `path` that details the path of
234+ * the response field which experienced the error. This allows clients to
235+ * identify whether a null result is intentional or caused by a runtime error.
236+ */
237+ + path : $ReadOnlyArray < string | number > | void ,
238+ /**
239+ * Reserved for implementors to extend the protocol however they see fit,
240+ * and hence there are no additional restrictions on its contents.
241+ */
242+ + extensions ?: { [ key : string ] : mixed , ... } ,
243+ | } ;
0 commit comments