Skip to content

Commit

Permalink
Issue graph-gophers#299: unclear error message in case of multiline s…
Browse files Browse the repository at this point in the history
…tring argument
  • Loading branch information
suntoucha committed Oct 7, 2020
1 parent c5bdf3b commit 43e20d2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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)
}
40 changes: 40 additions & 0 deletions internal/common/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,43 @@ 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())
}
})
}
}

0 comments on commit 43e20d2

Please sign in to comment.