Skip to content

GraphQLSyntaxErrorException instead of ArgumentOutOfRangeException #24

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

Merged
merged 2 commits into from
Mar 20, 2019
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
8 changes: 8 additions & 0 deletions src/GraphQLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using GraphQLParser;
using GraphQLParser.AST;
using GraphQLParser.Exceptions;
using System;
using System.Linq;
using Xunit;
Expand All @@ -10,6 +11,13 @@ public class ParserTests
{
private static readonly string NL = Environment.NewLine;

[Fact]
public void Parse_Unicode_Char_At_EOF_Should_Throw()
{
var parser = new Parser(new Lexer());
Assert.Throws<GraphQLSyntaxErrorException>(() => parser.Parse(new Source("{\"\\ue }")));
}

[Fact]
public void Parse_FieldInput_HasCorrectEndLocationAttribute()
{
Expand Down
8 changes: 7 additions & 1 deletion src/GraphQLParser/LexerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,14 @@ private int GetPositionAfterWhitespace(string body, int start)

private char GetUnicodeChar()
{
var expression = this.source.Body.Substring(this.currentIndex, 5);
if (this.currentIndex + 5 > this.source.Body.Length)
{
var truncatedExpression = this.source.Body.Substring(this.currentIndex);
throw new GraphQLSyntaxErrorException($"Invalid character escape sequence at EOF: \\{truncatedExpression}.", this.source, this.currentIndex);
}

var expression = this.source.Body.Substring(this.currentIndex, 5);

if (!this.OnlyHexInString(expression.Substring(1)))
{
throw new GraphQLSyntaxErrorException($"Invalid character escape sequence: \\{expression}.", this.source, this.currentIndex);
Expand Down