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

Issue #299: unclear error message in case of multiline string argument #412

Merged
merged 5 commits into from
Nov 13, 2020
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
10 changes: 9 additions & 1 deletion internal/common/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func NewLexer(s string, useStringDescriptions bool) *Lexer {
}
sc.Init(strings.NewReader(s))

return &Lexer{sc: sc, useStringDescriptions: useStringDescriptions}

l := Lexer{sc: sc, useStringDescriptions: useStringDescriptions}
l.sc.Error = l.CatchScannerError

return &l
}

func (l *Lexer) CatchSyntaxError(f func()) (errRes *errors.QueryError) {
Expand Down Expand Up @@ -219,3 +223,7 @@ func (l *Lexer) consumeComment() {
l.comment.WriteRune(next)
}
}

func (l *Lexer) CatchScannerError(s *scanner.Scanner, msg string) {
l.SyntaxError(msg)
}
39 changes: 39 additions & 0 deletions internal/common/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,42 @@ func TestConsume(t *testing.T) {
})
}
}

var multilineStringTests = []consumeTestCase {
{
description: "Oneline strings are okay",
definition: `"Hello World"`,
expected: "",
failureExpected: false,
useStringDescriptions: true,
},
{
description: "Multiline strings are not allowed",
definition: `"Hello
World"`,
expected: `graphql: syntax error: literal not terminated (line 1, column 1)`,
failureExpected: true,
useStringDescriptions: true,
},
}

func TestMultilineString(t *testing.T) {
for _, test := range multilineStringTests {
t.Run(test.description, func(t *testing.T) {
lex := common.NewLexer(test.definition, test.useStringDescriptions)

err := lex.CatchSyntaxError(func() { lex.ConsumeWhitespace() })
if test.failureExpected && err == nil {
t.Fatalf("Test '%s' should fail", test.description)
} else if test.failureExpected && err != nil {
if test.expected != err.Error() {
t.Fatalf("Test '%s' failed with wrong error: '%s'. Error should be: '%s'", test.description, err.Error(), test.expected)
}
}

if !test.failureExpected && err != nil {
t.Fatalf("Test '%s' failed with error: '%s'", test.description, err.Error())
}
})
}
}