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

Fix unicode bug with "format_raw_parse_error" sample #1044

Merged
merged 2 commits into from
Feb 10, 2021
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
3 changes: 2 additions & 1 deletion lib/absinthe/phase/parse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ defmodule Absinthe.Phase.Parse do
@spec format_raw_parse_error({:lexer, String.t(), {line :: pos_integer, column :: pos_integer}}) ::
Phase.Error.t()
defp format_raw_parse_error({:lexer, rest, {line, column}}) do
<<sample::binary-size(10), _::binary>> = rest
sample = String.slice(rest, 0, 10)

message = "Parsing failed at `#{sample}`"
%Phase.Error{message: message, locations: [%{line: line, column: column}], phase: __MODULE__}
end
Expand Down
28 changes: 28 additions & 0 deletions test/absinthe/phase/parse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ defmodule Absinthe.Phase.ParseTest do
] == bp.execution.validation_errors
end

@graphql "aa;bbbbbbbb—cc"
test "should provide sample of parsing failure respecting unicode boundary" do
assert {:error, bp} = Absinthe.Phase.Parse.run(@graphql, jump_phases: false)

assert [
%Absinthe.Phase.Error{
extra: %{},
locations: [%{column: 3, line: 1}],
message: "Parsing failed at `;bbbbbbbb—`",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're thinking character-oriented and not byte oriented I would expect 10 characters. In practice this test will fail because the message will include a sample of ten bytes.

phase: Absinthe.Phase.Parse
}
] == bp.execution.validation_errors
end

@graphql ";"
test "should provide sample of parsing failure on very short query strings" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something we experienced, but something that occurred to me when I was thinking about the binary pattern match.

assert {:error, bp} = Absinthe.Phase.Parse.run(@graphql, jump_phases: false)

assert [
%Absinthe.Phase.Error{
extra: %{},
locations: [%{column: 1, line: 1}],
message: "Parsing failed at `;`",
phase: Absinthe.Phase.Parse
}
] == bp.execution.validation_errors
end

@graphql """
query {
user {
Expand Down