Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions src/error/GraphQLError.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { ASTNode } from '../language/ast';
import { Source } from '../language/source';
import { SourceLocation } from '../language/location';

// eslint-disable-next-line import/no-cycle
import { GraphQLFormattedError } from './formatError';

/**
* Custom extensions
*
Expand Down Expand Up @@ -82,6 +85,10 @@ export class GraphQLError extends Error {
* Extension fields to add to the formatted error.
*/
readonly extensions: { [key: string]: any };

toString(): string;

toJSON(): GraphQLFormattedError;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/error/GraphQLError.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type { SourceLocation } from '../language/location';
import { getLocation } from '../language/location';
import { printLocation, printSourceLocation } from '../language/printLocation';

import { formatError } from './formatError';
import type { GraphQLFormattedError } from './formatError';

/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
Expand Down Expand Up @@ -157,6 +160,10 @@ export class GraphQLError extends Error {
return printError(this);
}

toJSON(): GraphQLFormattedError {
return formatError(this);
}

// FIXME: workaround to not break chai comparisons, should be remove in v16
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG](): string {
Expand Down
49 changes: 49 additions & 0 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,52 @@ describe('printError', () => {
`);
});
});

describe('toJSON', () => {
it('includes path', () => {
const error = new GraphQLError('msg', null, null, null, [
'path',
3,
'to',
'field',
]);

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: undefined,
path: ['path', 3, 'to', 'field'],
});
});

it('includes extension fields', () => {
const error = new GraphQLError('msg', null, null, null, null, null, {
foo: 'bar',
});

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: undefined,
path: undefined,
extensions: { foo: 'bar' },
});
});

it('can be created with full argument list', () => {
const error = new GraphQLError(
'msg',
[operationNode],
source,
[6],
['path', 2, 'a'],
new Error('I like turtles'),
{ hee: 'I like turtles' },
);

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: [{ column: 5, line: 2 }],
path: ['path', 2, 'a'],
extensions: { hee: 'I like turtles' },
});
});
});
1 change: 1 addition & 0 deletions src/error/formatError.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SourceLocation } from '../language/location';

// eslint-disable-next-line import/no-cycle
import { GraphQLError } from './GraphQLError';

/**
Expand Down
Loading