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
| Name | Type | Description |
|---|---|---|
| message | string | Human-readable error message. |
| options? | GraphQLErrorOptions | Configuration options for this operation. |
Returns
| Type | Description |
|---|---|
GraphQLError |
Constructor
Deprecated in v16Creates 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
| Name | Type | Description |
|---|---|---|
| message | string | Human-readable error message. |
| nodes? | ASTNode | readonly ASTNode[] | null | AST node or nodes associated with this error. |
| source? | null | undefined | Source | Source 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 | GraphQLErrorExtensions | Extension fields to include in the formatted error. |
Returns
| Type | Description |
|---|---|
GraphQLError |
Members
| Name | Type | Description |
|---|---|---|
| locations | readonly SourceLocation[] | undefined | An array of { line, column } locations within the source GraphQL documentwhich 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(). |
| path | readonly (string | number)[] | undefined | An 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(). |
| nodes | readonly ASTNode[] | undefined | An array of GraphQL AST Nodes corresponding to this error. |
| source | Source | undefined | The 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. |
| positions | readonly number[] | undefined | An array of character offsets within the source GraphQL document which correspond to this error. |
| originalError | Error | undefined | Original error that caused this GraphQLError, if one exists. |
| extensions | GraphQLErrorExtensions | Extension fields to add to the formatted error. |
toString()
Returns this error as a human-readable message with source locations.
Signature:
toString(): string
Returns
| Type | Description |
|---|---|
string | The formatted error string. |
Example
const message = error.toString();
// message: the formatted error messagetoJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): GraphQLFormattedError
Returns
| Type | Description |
|---|---|
GraphQLFormattedError | The JSON-serializable representation. |
Example
const json = error.toJSON();
// json: the JSON representationFunctions
printError()
Deprecated in v16Prints 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
| Name | Type | Description |
|---|---|---|
| error | GraphQLError | The error to format. |
Returns
| Type | Description |
|---|---|
string | The printed string representation. |
Example
import { GraphQLError, printError } from 'graphql/error';
const message = printError(new GraphQLError('Example error'));
// message: 'Example error'formatError()
Deprecated in v16Given 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
| Name | Type | Description |
|---|---|---|
| error | GraphQLError | The error to format. |
Returns
| Type | Description |
|---|---|
GraphQLFormattedError | The 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
| Name | Type | Description |
|---|---|---|
| rawOriginalError | unknown | The original error value to wrap. |
| nodes | ASTNode | readonly ASTNode[] | null | undefined | The AST nodes associated with the error. |
| path? | null | undefined | readonly (string | number)[] | The response path associated with the error. |
Returns
| Type | Description |
|---|---|
GraphQLError | The 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
| Name | Type | Description |
|---|---|---|
| source | Source | The GraphQL source text or source object. |
| position | number | The character offset in the source document. |
| description | string | The description value. |
Returns
| Type | Description |
|---|---|
GraphQLError | The 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
| Name | Type | Description |
|---|---|---|
| nodes? | ASTNode | readonly ASTNode[] | null | AST node or nodes associated with this error. |
| source? | null | undefined | Source | Source 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 | GraphQLErrorExtensions | Extension fields to include in the formatted result. |
GraphQLFormattedError
Interface. See: https://spec.graphql.org/draft/#sec-Errors
Members
| Name | Type | Description |
|---|---|---|
| message | string | A 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 ofthe response field which experienced the error. This allows clients to identify whether a null result is intentional or caused by a runtime error. |
| extensions? | GraphQLFormattedErrorExtensions | Reserved for implementors to extend the protocol however they see fit, and hence there are no additional restrictions on its contents. |