Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync tstypes/errors with flow #2105

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/error/formatError.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,31 @@ export function formatError(error: GraphQLError): GraphQLFormattedError {
: { message, locations, path };
}

/**
* @see https://github.com/graphql/graphql-spec/blob/master/spec/Section%207%20--%20Response.md#errors
*/
export type GraphQLFormattedError = {|
/**
* A short, human-readable summary of the problem that **SHOULD NOT** change
* from occurrence to occurrence of the problem, except for purposes of
* localization.
*/
+message: string,
/**
* If an error can be associated to a particular point in the requested
* GraphQL document, it should contain a list of locations.
*/
+locations: $ReadOnlyArray<SourceLocation> | void,
/**
* 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.
*/
+path: $ReadOnlyArray<string | number> | void,
/**
* Reserved for implementors to extend the protocol however they see fit,
* and hence there are no additional restrictions on its contents.
*/
+extensions?: { [key: string]: mixed, ... },
|};
33 changes: 21 additions & 12 deletions tstypes/error/GraphQLError.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Maybe from '../tsutils/Maybe';
import { getLocation } from '../language';

import { ASTNode } from '../language/ast';
import { Source } from '../language/source';
import { SourceLocation } from '../language/location';
import { SourceLocation, getLocation } from '../language/location';

/**
* A GraphQLError describes an Error found during the parse, validate, or
Expand All @@ -11,6 +11,16 @@ import { SourceLocation } from '../language/location';
* GraphQL document and/or execution result that correspond to the Error.
*/
export class GraphQLError extends Error {
constructor(
message: string,
nodes?: ReadonlyArray<ASTNode> | ASTNode | undefined,
source?: Maybe<Source>,
positions?: Maybe<ReadonlyArray<number>>,
path?: Maybe<ReadonlyArray<string | number>>,
originalError?: Maybe<Error>,
extensions?: Maybe<{ [key: string]: any }>,
);

/**
* A message describing the Error for debugging purposes.
*
Expand Down Expand Up @@ -47,6 +57,9 @@ export class GraphQLError extends Error {

/**
* The source GraphQL document corresponding to this error.
*
* Note that if this Error represents more than one node, the source may not
* represent nodes after the first node.
*/
readonly source: Source | undefined;

Expand All @@ -65,14 +78,10 @@ export class GraphQLError extends Error {
* Extension fields to add to the formatted error.
*/
readonly extensions: { [key: string]: any } | undefined;

constructor(
message: string,
nodes?: ReadonlyArray<ASTNode> | ASTNode | undefined,
source?: Maybe<Source>,
positions?: Maybe<ReadonlyArray<number>>,
path?: Maybe<ReadonlyArray<string | number>>,
originalError?: Maybe<Error>,
extensions?: Maybe<{ [key: string]: any }>,
);
}

/**
* Prints a GraphQLError to a string, representing useful location information
* about the error's position in the source.
*/
export function printError(error: GraphQLError): string;
2 changes: 1 addition & 1 deletion tstypes/error/formatError.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLError } from './GraphQLError';
import { SourceLocation } from '../language/location';
import { GraphQLError } from './GraphQLError';

/**
* Given a GraphQLError, format it according to the rules described by the
Expand Down
3 changes: 1 addition & 2 deletions tstypes/error/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { GraphQLError } from './GraphQLError';
export { GraphQLError, printError } from './GraphQLError';
export { syntaxError } from './syntaxError';
export { locatedError } from './locatedError';
export { printError } from './printError';
export { formatError, GraphQLFormattedError } from './formatError';
2 changes: 1 addition & 1 deletion tstypes/error/locatedError.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLError } from './GraphQLError';
import { ASTNode } from '../language/ast';
import { GraphQLError } from './GraphQLError';

/**
* Given an arbitrary Error, presumably thrown while attempting to execute a
Expand Down
7 changes: 0 additions & 7 deletions tstypes/error/printError.d.ts

This file was deleted.