Skip to content

Commit cb69e09

Browse files
parser: Convert error messages to match common standard of this… (#2175)
1 parent a438dd0 commit cb69e09

File tree

7 files changed

+66
-55
lines changed

7 files changed

+66
-55
lines changed

src/execution/__tests__/sync-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('Execute: synchronously when possible', () => {
102102
expect(result).to.deep.equal({
103103
errors: [
104104
{
105-
message: 'Syntax Error: Expected Name, found {',
105+
message: 'Syntax Error: Expected Name, found "{".',
106106
locations: [{ line: 1, column: 29 }],
107107
},
108108
],

src/language/__tests__/lexer-test.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { GraphQLError } from '../../error/GraphQLError';
1212

1313
import { Source } from '../source';
1414
import { TokenKind } from '../tokenKind';
15-
import { createLexer, isPunctuatorToken } from '../lexer';
15+
import { createLexer, isPunctuatorTokenKind } from '../lexer';
1616

1717
function lexOne(str) {
1818
const lexer = createLexer(new Source(str));
@@ -905,30 +905,34 @@ describe('Lexer', () => {
905905
});
906906
});
907907

908-
describe('isPunctuatorToken', () => {
908+
describe('isPunctuatorTokenKind', () => {
909+
function isPunctuatorToken(text) {
910+
return isPunctuatorTokenKind(lexOne(text).kind);
911+
}
912+
909913
it('returns true for punctuator tokens', () => {
910-
expect(isPunctuatorToken(lexOne('!'))).to.equal(true);
911-
expect(isPunctuatorToken(lexOne('$'))).to.equal(true);
912-
expect(isPunctuatorToken(lexOne('&'))).to.equal(true);
913-
expect(isPunctuatorToken(lexOne('('))).to.equal(true);
914-
expect(isPunctuatorToken(lexOne(')'))).to.equal(true);
915-
expect(isPunctuatorToken(lexOne('...'))).to.equal(true);
916-
expect(isPunctuatorToken(lexOne(':'))).to.equal(true);
917-
expect(isPunctuatorToken(lexOne('='))).to.equal(true);
918-
expect(isPunctuatorToken(lexOne('@'))).to.equal(true);
919-
expect(isPunctuatorToken(lexOne('['))).to.equal(true);
920-
expect(isPunctuatorToken(lexOne(']'))).to.equal(true);
921-
expect(isPunctuatorToken(lexOne('{'))).to.equal(true);
922-
expect(isPunctuatorToken(lexOne('|'))).to.equal(true);
923-
expect(isPunctuatorToken(lexOne('}'))).to.equal(true);
914+
expect(isPunctuatorToken('!')).to.equal(true);
915+
expect(isPunctuatorToken('$')).to.equal(true);
916+
expect(isPunctuatorToken('&')).to.equal(true);
917+
expect(isPunctuatorToken('(')).to.equal(true);
918+
expect(isPunctuatorToken(')')).to.equal(true);
919+
expect(isPunctuatorToken('...')).to.equal(true);
920+
expect(isPunctuatorToken(':')).to.equal(true);
921+
expect(isPunctuatorToken('=')).to.equal(true);
922+
expect(isPunctuatorToken('@')).to.equal(true);
923+
expect(isPunctuatorToken('[')).to.equal(true);
924+
expect(isPunctuatorToken(']')).to.equal(true);
925+
expect(isPunctuatorToken('{')).to.equal(true);
926+
expect(isPunctuatorToken('|')).to.equal(true);
927+
expect(isPunctuatorToken('}')).to.equal(true);
924928
});
925929

926930
it('returns false for non-punctuator tokens', () => {
927-
expect(isPunctuatorToken(lexOne(''))).to.equal(false);
928-
expect(isPunctuatorToken(lexOne('name'))).to.equal(false);
929-
expect(isPunctuatorToken(lexOne('1'))).to.equal(false);
930-
expect(isPunctuatorToken(lexOne('3.14'))).to.equal(false);
931-
expect(isPunctuatorToken(lexOne('"str"'))).to.equal(false);
932-
expect(isPunctuatorToken(lexOne('"""str"""'))).to.equal(false);
931+
expect(isPunctuatorToken('')).to.equal(false);
932+
expect(isPunctuatorToken('name')).to.equal(false);
933+
expect(isPunctuatorToken('1')).to.equal(false);
934+
expect(isPunctuatorToken('3.14')).to.equal(false);
935+
expect(isPunctuatorToken('"str"')).to.equal(false);
936+
expect(isPunctuatorToken('"""str"""')).to.equal(false);
933937
});
934938
});

src/language/__tests__/parser-test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ describe('Parser', () => {
4040
}
4141

4242
expect(caughtError).to.deep.contain({
43-
message: 'Syntax Error: Expected Name, found <EOF>',
43+
message: 'Syntax Error: Expected Name, found <EOF>.',
4444
positions: [1],
4545
locations: [{ line: 1, column: 2 }],
4646
});
4747

4848
expect(String(caughtError) + '\n').to.equal(dedent`
49-
Syntax Error: Expected Name, found <EOF>
49+
Syntax Error: Expected Name, found <EOF>.
5050
5151
GraphQL request:1:2
5252
1 | {
@@ -57,22 +57,22 @@ describe('Parser', () => {
5757
{ ...MissingOn }
5858
fragment MissingOn Type
5959
`).to.deep.include({
60-
message: 'Syntax Error: Expected "on", found Name "Type"',
60+
message: 'Syntax Error: Expected "on", found Name "Type".',
6161
locations: [{ line: 3, column: 26 }],
6262
});
6363

6464
expectSyntaxError('{ field: {} }').to.deep.include({
65-
message: 'Syntax Error: Expected Name, found {',
65+
message: 'Syntax Error: Expected Name, found "{".',
6666
locations: [{ line: 1, column: 10 }],
6767
});
6868

6969
expectSyntaxError('notanoperation Foo { field }').to.deep.include({
70-
message: 'Syntax Error: Unexpected Name "notanoperation"',
70+
message: 'Syntax Error: Unexpected Name "notanoperation".',
7171
locations: [{ line: 1, column: 1 }],
7272
});
7373

7474
expectSyntaxError('...').to.deep.include({
75-
message: 'Syntax Error: Unexpected ...',
75+
message: 'Syntax Error: Unexpected "...".',
7676
locations: [{ line: 1, column: 1 }],
7777
});
7878
});
@@ -85,7 +85,7 @@ describe('Parser', () => {
8585
caughtError = error;
8686
}
8787
expect(String(caughtError) + '\n').to.equal(dedent`
88-
Syntax Error: Expected {, found <EOF>
88+
Syntax Error: Expected "{", found <EOF>.
8989
9090
MyQuery.graphql:1:6
9191
1 | query
@@ -103,7 +103,7 @@ describe('Parser', () => {
103103
expectSyntaxError(
104104
'query Foo($x: Complex = { a: { b: [ $var ] } }) { field }',
105105
).to.deep.equal({
106-
message: 'Syntax Error: Unexpected $',
106+
message: 'Syntax Error: Unexpected "$".',
107107
locations: [{ line: 1, column: 37 }],
108108
});
109109
});
@@ -116,14 +116,14 @@ describe('Parser', () => {
116116

117117
it('does not accept fragments named "on"', () => {
118118
expectSyntaxError('fragment on on on { on }').to.deep.equal({
119-
message: 'Syntax Error: Unexpected Name "on"',
119+
message: 'Syntax Error: Unexpected Name "on".',
120120
locations: [{ line: 1, column: 10 }],
121121
});
122122
});
123123

124124
it('does not accept fragments spread of "on"', () => {
125125
expectSyntaxError('{ ...on }').to.deep.equal({
126-
message: 'Syntax Error: Expected Name, found }',
126+
message: 'Syntax Error: Expected Name, found "}".',
127127
locations: [{ line: 1, column: 9 }],
128128
});
129129
});

src/language/__tests__/schema-parser-test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe('Schema Parser', () => {
221221

222222
it('Extension without anything throws', () => {
223223
expectSyntaxError('extend type Hello').to.deep.equal({
224-
message: 'Syntax Error: Unexpected <EOF>',
224+
message: 'Syntax Error: Unexpected <EOF>.',
225225
locations: [{ line: 1, column: 18 }],
226226
});
227227
});
@@ -233,7 +233,7 @@ describe('Schema Parser', () => {
233233
world: String
234234
}
235235
`).to.deep.equal({
236-
message: 'Syntax Error: Unexpected Name "extend"',
236+
message: 'Syntax Error: Unexpected Name "extend".',
237237
locations: [{ line: 3, column: 7 }],
238238
});
239239

@@ -242,7 +242,7 @@ describe('Schema Parser', () => {
242242
world: String
243243
}
244244
`).to.deep.equal({
245-
message: 'Syntax Error: Unexpected String "Description"',
245+
message: 'Syntax Error: Unexpected String "Description".',
246246
locations: [{ line: 2, column: 14 }],
247247
});
248248
});
@@ -300,7 +300,7 @@ describe('Schema Parser', () => {
300300

301301
it('Schema extension without anything throws', () => {
302302
expectSyntaxError('extend schema').to.deep.equal({
303-
message: 'Syntax Error: Unexpected <EOF>',
303+
message: 'Syntax Error: Unexpected <EOF>.',
304304
locations: [{ line: 1, column: 14 }],
305305
});
306306
});
@@ -724,28 +724,28 @@ describe('Schema Parser', () => {
724724

725725
it('Union fails with no types', () => {
726726
expectSyntaxError('union Hello = |').to.deep.equal({
727-
message: 'Syntax Error: Expected Name, found <EOF>',
727+
message: 'Syntax Error: Expected Name, found <EOF>.',
728728
locations: [{ line: 1, column: 16 }],
729729
});
730730
});
731731

732732
it('Union fails with leading double pipe', () => {
733733
expectSyntaxError('union Hello = || Wo | Rld').to.deep.equal({
734-
message: 'Syntax Error: Expected Name, found |',
734+
message: 'Syntax Error: Expected Name, found "|".',
735735
locations: [{ line: 1, column: 16 }],
736736
});
737737
});
738738

739739
it('Union fails with double pipe', () => {
740740
expectSyntaxError('union Hello = Wo || Rld').to.deep.equal({
741-
message: 'Syntax Error: Expected Name, found |',
741+
message: 'Syntax Error: Expected Name, found "|".',
742742
locations: [{ line: 1, column: 19 }],
743743
});
744744
});
745745

746746
it('Union fails with trailing pipe', () => {
747747
expectSyntaxError('union Hello = | Wo | Rld |').to.deep.equal({
748-
message: 'Syntax Error: Expected Name, found <EOF>',
748+
message: 'Syntax Error: Expected Name, found <EOF>.',
749749
locations: [{ line: 1, column: 27 }],
750750
});
751751
});
@@ -803,7 +803,7 @@ input Hello {
803803
world(foo: Int): String
804804
}
805805
`).to.deep.equal({
806-
message: 'Syntax Error: Expected :, found (',
806+
message: 'Syntax Error: Expected ":", found "(".',
807807
locations: [{ line: 3, column: 14 }],
808808
});
809809
});
@@ -884,7 +884,7 @@ input Hello {
884884
expectSyntaxError(
885885
'directive @foo on FIELD | INCORRECT_LOCATION',
886886
).to.deep.equal({
887-
message: 'Syntax Error: Unexpected Name "INCORRECT_LOCATION"',
887+
message: 'Syntax Error: Unexpected Name "INCORRECT_LOCATION".',
888888
locations: [{ line: 1, column: 27 }],
889889
});
890890
});
@@ -896,7 +896,7 @@ input Hello {
896896
it('Option: allowLegacySDLEmptyFields supports type with empty fields', () => {
897897
const body = 'type Hello { }';
898898
expectSyntaxError(body).to.include({
899-
message: 'Syntax Error: Expected Name, found }',
899+
message: 'Syntax Error: Expected Name, found "}".',
900900
});
901901

902902
const doc = parse(body, { allowLegacySDLEmptyFields: true });
@@ -906,7 +906,7 @@ input Hello {
906906
it('Option: allowLegacySDLImplementsInterfaces', () => {
907907
const body = 'type Hello implements Wo rld { field: String }';
908908
expectSyntaxError(body).to.include({
909-
message: 'Syntax Error: Unexpected Name "rld"',
909+
message: 'Syntax Error: Unexpected Name "rld".',
910910
});
911911

912912
const doc = parse(body, { allowLegacySDLImplementsInterfaces: true });

src/language/lexer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ export type Lexer<TOptions> = {
9494
};
9595

9696
// @internal
97-
export function isPunctuatorToken(token: Token) {
98-
const kind = token.kind;
97+
export function isPunctuatorTokenKind(kind: TokenKindEnum) {
9998
return (
10099
kind === TokenKind.BANG ||
101100
kind === TokenKind.DOLLAR ||

src/language/parser.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { type GraphQLError } from '../error/GraphQLError';
99

1010
import { Kind } from './kinds';
1111
import { Source } from './source';
12-
import { type Lexer, createLexer } from './lexer';
1312
import { DirectiveLocation } from './directiveLocation';
1413
import { type TokenKindEnum, TokenKind } from './tokenKind';
14+
import { type Lexer, createLexer, isPunctuatorTokenKind } from './lexer';
1515
import {
1616
type Location,
1717
type Token,
@@ -1418,7 +1418,7 @@ class Parser {
14181418
throw syntaxError(
14191419
this._lexer.source,
14201420
token.start,
1421-
`Expected ${kind}, found ${getTokenDesc(token)}`,
1421+
`Expected ${getTokenKindDesc(kind)}, found ${getTokenDesc(token)}.`,
14221422
);
14231423
}
14241424

@@ -1447,7 +1447,7 @@ class Parser {
14471447
throw syntaxError(
14481448
this._lexer.source,
14491449
token.start,
1450-
`Expected "${value}", found ${getTokenDesc(token)}`,
1450+
`Expected "${value}", found ${getTokenDesc(token)}.`,
14511451
);
14521452
}
14531453
}
@@ -1474,7 +1474,7 @@ class Parser {
14741474
return syntaxError(
14751475
this._lexer.source,
14761476
token.start,
1477-
`Unexpected ${getTokenDesc(token)}`,
1477+
`Unexpected ${getTokenDesc(token)}.`,
14781478
);
14791479
}
14801480

@@ -1556,6 +1556,14 @@ defineToJSON(Loc, function() {
15561556
* A helper function to describe a token as a string for debugging
15571557
*/
15581558
function getTokenDesc(token: Token): string {
1559-
const value = token.value;
1560-
return value ? `${token.kind} "${value}"` : token.kind;
1559+
return (
1560+
getTokenKindDesc(token.kind) + (token.value ? ` "${token.value}"` : '')
1561+
);
1562+
}
1563+
1564+
/**
1565+
* A helper function to describe a token kind as a string for debugging
1566+
*/
1567+
function getTokenKindDesc(kind: TokenKindEnum): string {
1568+
return isPunctuatorTokenKind(kind) ? `"${kind}"` : kind;
15611569
}

src/utilities/stripIgnoredCharacters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import inspect from '../jsutils/inspect';
44

55
import { Source } from '../language/source';
66
import { TokenKind } from '../language/tokenKind';
7-
import { createLexer, isPunctuatorToken } from '../language/lexer';
7+
import { createLexer, isPunctuatorTokenKind } from '../language/lexer';
88
import {
99
dedentBlockStringValue,
1010
getBlockStringIndentation,
@@ -84,7 +84,7 @@ export function stripIgnoredCharacters(source: string | Source): string {
8484
* Also prevent case of non-punctuator token following by spread resulting
8585
* in invalid token (e.g. `1...` is invalid Float token).
8686
*/
87-
const isNonPunctuator = !isPunctuatorToken(currentToken);
87+
const isNonPunctuator = !isPunctuatorTokenKind(currentToken.kind);
8888
if (wasLastAddedTokenNonPunctuator) {
8989
if (isNonPunctuator || currentToken.kind === TokenKind.SPREAD) {
9090
strippedBody += ' ';

0 commit comments

Comments
 (0)