Skip to content

format_error should return locations as a list of {"line", "column"} #62

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 3 commits into from
Sep 25, 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
6 changes: 5 additions & 1 deletion src/graphql/error/format_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def format_error(error: "GraphQLError") -> Dict[str, Any]:
raise TypeError("Received no error object.")
formatted: Dict[str, Any] = dict( # noqa: E701 (pycqa/flake8#394)
message=error.message or "An unknown error occurred.",
locations=error.locations,
locations=(
[location.formatted for location in error.locations]
if error.locations is not None
else None
),
path=error.path,
)
if error.extensions:
Expand Down
12 changes: 12 additions & 0 deletions src/graphql/language/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ class SourceLocation(NamedTuple):
line: int
column: int

@property
def formatted(self):
return dict(line=self.line, column=self.column)

def __eq__(self, other):
if isinstance(other, dict):
return other == self.formatted
return super().__eq__(other)

def __ne__(self, other):
return not self == other


def get_location(source: "Source", position: int) -> SourceLocation:
"""Get the line and column for a character position in the source.
Expand Down
5 changes: 3 additions & 2 deletions tests/error/test_format_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def format_graphql_error():
ValueError("original"),
extensions=extensions,
)
assert error == {
formatted = format_error(error)
assert formatted == {
"message": "test message",
"locations": [(2, 14), (3, 20)],
"locations": [{"line": 2, "column": 14}, {"line": 3, "column": 20}],
"path": path,
"extensions": extensions,
}
Expand Down
4 changes: 2 additions & 2 deletions tests/execution/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def resolve_type_on_interface_yields_useful_error():
assert format_error(result.errors[0]) == {
"message": "Runtime Object type 'Human'"
" is not a possible type for 'Pet'.",
"locations": [(3, 15)],
"locations": [{"line": 3, "column": 15}],
"path": ["pets", 2],
}

Expand Down Expand Up @@ -330,7 +330,7 @@ def resolve_type_on_union_yields_useful_error():
assert format_error(result.errors[0]) == {
"message": "Runtime Object type 'Human'"
" is not a possible type for 'Pet'.",
"locations": [(3, 15)],
"locations": [{"line": 3, "column": 15}],
"path": ["pets", 2],
}

Expand Down
8 changes: 4 additions & 4 deletions tests/execution/test_abstract_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ async def is_type_of_with_async_error():
assert list(map(format_error, result.errors)) == [ # type: ignore
{
"message": "We are testing this error",
"locations": [(3, 15)],
"locations": [{"line": 3, "column": 15}],
"path": ["pets", 0],
},
{
"message": "We are testing this error",
"locations": [(3, 15)],
"locations": [{"line": 3, "column": 15}],
"path": ["pets", 1],
},
]
Expand Down Expand Up @@ -334,7 +334,7 @@ async def resolve_type_on_interface_yields_useful_error():
assert format_error(result.errors[0]) == {
"message": "Runtime Object type 'Human'"
" is not a possible type for 'Pet'.",
"locations": [(3, 15)],
"locations": [{"line": 3, "column": 15}],
"path": ["pets", 2],
}

Expand Down Expand Up @@ -411,7 +411,7 @@ async def resolve_type_on_union_yields_useful_error():
assert format_error(result.errors[0]) == {
"message": "Runtime Object type 'Human'"
" is not a possible type for 'Pet'.",
"locations": [(3, 15)],
"locations": [{"line": 3, "column": 15}],
"path": ["pets", 2],
}

Expand Down
12 changes: 12 additions & 0 deletions tests/language/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from graphql import SourceLocation


def describe_SourceLocation():
def equals_with_itself():
assert SourceLocation(1, 2) == SourceLocation(1, 2)
assert (SourceLocation(1, 2) != SourceLocation(1, 2)) is False

def equals_with_formatted_form():
sl = SourceLocation(1, 2)
assert SourceLocation(1, 2) == sl.formatted
assert (SourceLocation(1, 2) != sl.formatted) is False