Skip to content

Commit 9c36bba

Browse files
authored
Merge pull request #412 from suntoucha/master
Issue #299: unclear error message in case of multiline string argument
2 parents d5f4f41 + 4af9f16 commit 9c36bba

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

internal/common/lexer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ func NewLexer(s string, useStringDescriptions bool) *Lexer {
3030
}
3131
sc.Init(strings.NewReader(s))
3232

33-
return &Lexer{sc: sc, useStringDescriptions: useStringDescriptions}
33+
34+
l := Lexer{sc: sc, useStringDescriptions: useStringDescriptions}
35+
l.sc.Error = l.CatchScannerError
36+
37+
return &l
3438
}
3539

3640
func (l *Lexer) CatchSyntaxError(f func()) (errRes *errors.QueryError) {
@@ -219,3 +223,7 @@ func (l *Lexer) consumeComment() {
219223
l.comment.WriteRune(next)
220224
}
221225
}
226+
227+
func (l *Lexer) CatchScannerError(s *scanner.Scanner, msg string) {
228+
l.SyntaxError(msg)
229+
}

internal/common/lexer_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,42 @@ func TestConsume(t *testing.T) {
9393
})
9494
}
9595
}
96+
97+
var multilineStringTests = []consumeTestCase {
98+
{
99+
description: "Oneline strings are okay",
100+
definition: `"Hello World"`,
101+
expected: "",
102+
failureExpected: false,
103+
useStringDescriptions: true,
104+
},
105+
{
106+
description: "Multiline strings are not allowed",
107+
definition: `"Hello
108+
World"`,
109+
expected: `graphql: syntax error: literal not terminated (line 1, column 1)`,
110+
failureExpected: true,
111+
useStringDescriptions: true,
112+
},
113+
}
114+
115+
func TestMultilineString(t *testing.T) {
116+
for _, test := range multilineStringTests {
117+
t.Run(test.description, func(t *testing.T) {
118+
lex := common.NewLexer(test.definition, test.useStringDescriptions)
119+
120+
err := lex.CatchSyntaxError(func() { lex.ConsumeWhitespace() })
121+
if test.failureExpected && err == nil {
122+
t.Fatalf("Test '%s' should fail", test.description)
123+
} else if test.failureExpected && err != nil {
124+
if test.expected != err.Error() {
125+
t.Fatalf("Test '%s' failed with wrong error: '%s'. Error should be: '%s'", test.description, err.Error(), test.expected)
126+
}
127+
}
128+
129+
if !test.failureExpected && err != nil {
130+
t.Fatalf("Test '%s' failed with error: '%s'", test.description, err.Error())
131+
}
132+
})
133+
}
134+
}

0 commit comments

Comments
 (0)