graphql/error
模組負責建立和格式化 GraphQL 錯誤。您可以從 graphql/error
模組或根 graphql
模組匯入。例如
import { GraphQLError } from "graphql" // ES6var { GraphQLError } = require("graphql") // CommonJS
class GraphQLError extends Error { constructor( message: string, nodes?: Array<any>, stack?: ?string, source?: Source, positions?: Array<number>, originalError?: ?Error, extensions?: ?{ [key: string]: mixed } )}
GraphQL 中發生的錯誤表示。包含有關錯誤發生在查詢中何處的資訊,以進行偵錯。最常與下方的 locatedError
一起建構。
function syntaxError( source: Source, position: number, description: string): GraphQLError;
產生表示語法錯誤的 GraphQLError,包含有關語法錯誤在來源中位置的有用描述性資訊。
function locatedError(error: ?Error, nodes: Array<any>): GraphQLError {
假設執行 GraphQL 作業時發生任意錯誤,產生新的 GraphQLError,並注意文件中的位置,造成原始錯誤。
function formatError(error: GraphQLError): GraphQLFormattedError
type GraphQLFormattedError = { message: string, locations: ?Array<GraphQLErrorLocation>};
type GraphQLErrorLocation = { line: number, column: number};
給定一個 GraphQLError,根據 GraphQL 規格的回應格式,錯誤部分所述的規則格式化它。