graphql/error/errors

Classes

GraphQLError

A GraphQLError describes an Error found during the parse, validate, or execute phases of performing a GraphQL operation. In addition to a message and stack trace, it also includes information about the locations in a GraphQL document and/or execution result that correspond to the Error.

Constructor

Creates a GraphQLError instance.

Signature:

new GraphQLError(message: string, options?: GraphQLErrorOptions)
Arguments
NameTypeDescription
messagestringHuman-readable error message.
options?GraphQLErrorOptionsConfiguration options for this operation.
Returns
TypeDescription
GraphQLError

Constructor

Deprecated in v16

Creates a GraphQLError instance using the legacy positional constructor. Prefer the GraphQLErrorOptions object overload, which keeps optional error metadata in a single options bag.

Signature:

new GraphQLError(message: string, nodes?: ASTNode | readonly ASTNode[] | null, source?: null | undefined | Source, positions?: null | undefined | readonly number[], path?: null | undefined | readonly (string | number)[], originalError?: null | undefined | Error & { extensions?: unknown }, extensions?: null | undefined | GraphQLErrorExtensions)
Arguments
NameTypeDescription
messagestringHuman-readable error message.
nodes?ASTNode | readonly ASTNode[] | nullAST node or nodes associated with this error.
source?null | undefined | SourceSource document used to derive error locations.
positions?null | undefined | readonly number[]Character offsets in the source document associated with
this error.
path?null | undefined | readonly (string | number)[]Response path where this error occurred during execution.
originalError?null | undefined | Error & { extensions?: unknown }Original error that caused this GraphQLError, if one
exists.
extensions?null | undefined | GraphQLErrorExtensionsExtension fields to include in the formatted error.
Returns
TypeDescription
GraphQLError

Members

NameTypeDescription
locationsreadonly SourceLocation[] | undefinedAn array of { line, column } locations within the source GraphQL document
which correspond to this error.
Errors during validation often contain multiple locations, for example to
point out two things with the same name. Errors during execution include a
single location, the field which produced the error.
Enumerable, and appears in the result of JSON.stringify().
pathreadonly (string | number)[] | undefinedAn array describing the JSON-path into the execution response which
corresponds to this error. Only included for errors during execution.
Enumerable, and appears in the result of JSON.stringify().
nodesreadonly ASTNode[] | undefinedAn array of GraphQL AST Nodes corresponding to this error.
sourceSource | undefinedThe source GraphQL document for the first location of this error.
Note that if this Error represents more than one node, the source may not
represent nodes after the first node.
positionsreadonly number[] | undefinedAn array of character offsets within the source GraphQL document
which correspond to this error.
originalErrorError | undefinedOriginal error that caused this GraphQLError, if one exists.
extensionsGraphQLErrorExtensionsExtension fields to add to the formatted error.

toString()

Returns this error as a human-readable message with source locations.

Signature:

toString(): string
Returns
TypeDescription
stringThe formatted error string.
Example
const message = error.toString();
 
// message: the formatted error message

toJSON()

Returns the JSON representation used when this object is serialized.

Signature:

toJSON(): GraphQLFormattedError
Returns
TypeDescription
GraphQLFormattedErrorThe JSON-serializable representation.
Example
const json = error.toJSON();
 
// json: the JSON representation

Functions

printError()

Deprecated in v16

Prints a GraphQLError to a string, representing useful location information about the error’s position in the source. This helper is retained for backwards compatibility; call error.toString() instead because printError will be removed in v17.

Signature:

printError(error: GraphQLError): string

Arguments

NameTypeDescription
errorGraphQLErrorThe error to format.

Returns

TypeDescription
stringThe printed string representation.

Example

import { GraphQLError, printError } from 'graphql/error';
 
const message = printError(new GraphQLError('Example error'));
 
// message: 'Example error'

formatError()

Deprecated in v16

Given a GraphQLError, format it according to the rules described by the Response Format, Errors section of the GraphQL Specification. This helper is retained for backwards compatibility; call error.toJSON() instead because formatError will be removed in v17.

Signature:

formatError(error: GraphQLError): GraphQLFormattedError

Arguments

NameTypeDescription
errorGraphQLErrorThe error to format.

Returns

TypeDescription
GraphQLFormattedErrorThe JSON-serializable formatted error.

Example

import { GraphQLError, formatError } from 'graphql/error';
 
const formatted = formatError(new GraphQLError('Example error'));
 
// formatted.message: 'Example error'

locatedError()

Given an arbitrary value, presumably thrown while attempting to execute a GraphQL operation, produce a new GraphQLError aware of the location in the document responsible for the original Error.

Signature:

locatedError(rawOriginalError: unknown, nodes: ASTNode | readonly ASTNode[] | null | undefined, path?: null | undefined | readonly (string | number)[]): GraphQLError

Arguments

NameTypeDescription
rawOriginalErrorunknownThe original error value to wrap.
nodesASTNode | readonly ASTNode[] | null | undefinedThe AST nodes associated with the error.
path?null | undefined | readonly (string | number)[]The response path associated with the error.

Returns

TypeDescription
GraphQLErrorThe GraphQL error.

Example

import { locatedError } from 'graphql/error';
 
const error = locatedError(new Error('Resolver failed'));
 
// error.message: 'Resolver failed'

syntaxError()

Produces a GraphQLError representing a syntax error, containing useful descriptive information about the syntax error’s position in the source.

Signature:

syntaxError(source: Source, position: number, description: string): GraphQLError

Arguments

NameTypeDescription
sourceSourceThe GraphQL source text or source object.
positionnumberThe character offset in the source document.
descriptionstringThe description value.

Returns

TypeDescription
GraphQLErrorThe GraphQL error.

Example

import { Source } from 'graphql/language';
import { syntaxError } from 'graphql/error';
 
const error = syntaxError(new Source('query {'), 7, 'Expected Name');
 
// error.message: 'Syntax Error: Expected Name'

Types

GraphQLErrorExtensions

Interface. Custom extensions

Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.

GraphQLFormattedErrorExtensions

Interface. Custom formatted extensions

Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.

GraphQLErrorOptions

Interface. Options used to construct a GraphQLError.

Members

NameTypeDescription
nodes?ASTNode | readonly ASTNode[] | nullAST node or nodes associated with this error.
source?null | undefined | SourceSource document used to derive error locations.
positions?null | undefined | readonly number[]Character offsets in the source document associated with this error.
path?null | undefined | readonly (string | number)[]Response path where this error occurred during execution.
originalError?null | undefined | Error & { extensions?: unknown }Original error that caused this GraphQLError, if one exists.
extensions?null | undefined | GraphQLErrorExtensionsExtension fields to include in the formatted result.

GraphQLFormattedError

Interface. See: https://spec.graphql.org/draft/#sec-Errors

Members

NameTypeDescription
messagestringA short, human-readable summary of the problem that SHOULD NOT change
from occurrence to occurrence of the problem, except for purposes of
localization.
locations?readonly SourceLocation[]If an error can be associated to a particular point in the requested
GraphQL document, it should contain a list of locations.
path?readonly (string | number)[]If an error can be associated to a particular field in the GraphQL result,
it must contain an entry with the key path that details the path of
the response field which experienced the error. This allows clients to
identify whether a null result is intentional or caused by a runtime error.
extensions?GraphQLFormattedErrorExtensionsReserved for implementors to extend the protocol however they see fit,
and hence there are no additional restrictions on its contents.