GraphQL.JS 文档
graphql/错误

graphql/error

graphql/error 模块负责创建和格式化 GraphQL 错误。你可以从 graphql/error 模块或根 graphql 模块导入。例如:

¥The graphql/error module is responsible for creating and formatting GraphQL errors. You can import either from the graphql/error module, or from the root graphql module. For example:

import { GraphQLError } from "graphql" // ES6
var { GraphQLError } = require("graphql") // CommonJS

概述

¥Overview

错误

¥Errors

GraphQLError

class GraphQLError extends Error {
 constructor(
   message: string,
   nodes?: any[],
   stack?: string,
   source?: Source,
   positions?: number[],
   originalError?: Error,
   extensions?: { [key: string]: mixed }
 )
}

GraphQL 中发生的错误的表示。包含有关查询中发生错误的位置的信息以供调试。最常用的是下面的 locatedError

¥A representation of an error that occurred within GraphQL. Contains information about where in the query the error occurred for debugging. Most commonly constructed with locatedError below.

syntaxError

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

生成一个表示语法错误的 GraphQLError,其中包含有关语法错误在源中的位置的有用描述信息。

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

locatedError

function locatedError(error: Error, nodes: any[]): GraphQLError

给定一个任意错误(可能是在尝试执行 GraphQL 操作时抛出的),生成一个新的 GraphQLError,了解文档中导致原始错误的位置。

¥Given an arbitrary Error, 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.

formatError

function formatError(error: GraphQLError): GraphQLFormattedError
 
type GraphQLFormattedError = {
  message: string,
  locations: GraphQLErrorLocation[]
};
 
type GraphQLErrorLocation = {
  line: number,
  column: number
};

给定 GraphQLError,根据 GraphQL 规范的响应格式、错误部分描述的规则对其进行格式化。

¥Given a GraphQLError, format it according to the rules described by the Response Format, Errors section of the GraphQL Specification.