Skip to content

Commit ca0f9e6

Browse files
ktosiekCito
authored andcommitted
format_error should return locations as a list of dicts (#62)
1 parent ae7ae54 commit ca0f9e6

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

src/graphql/error/format_error.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ def format_error(error: "GraphQLError") -> Dict[str, Any]:
1717
raise TypeError("Received no error object.")
1818
formatted: Dict[str, Any] = dict( # noqa: E701 (pycqa/flake8#394)
1919
message=error.message or "An unknown error occurred.",
20-
locations=error.locations,
20+
locations=(
21+
[location.formatted for location in error.locations]
22+
if error.locations is not None
23+
else None
24+
),
2125
path=error.path,
2226
)
2327
if error.extensions:

src/graphql/language/location.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ class SourceLocation(NamedTuple):
1212
line: int
1313
column: int
1414

15+
@property
16+
def formatted(self):
17+
return dict(line=self.line, column=self.column)
18+
19+
def __eq__(self, other):
20+
if isinstance(other, dict):
21+
return other == self.formatted
22+
return super().__eq__(other)
23+
24+
def __ne__(self, other):
25+
return not self == other
26+
1527

1628
def get_location(source: "Source", position: int) -> SourceLocation:
1729
"""Get the line and column for a character position in the source.

tests/error/test_format_error.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ def format_graphql_error():
3030
ValueError("original"),
3131
extensions=extensions,
3232
)
33-
assert error == {
33+
formatted = format_error(error)
34+
assert formatted == {
3435
"message": "test message",
35-
"locations": [(2, 14), (3, 20)],
36+
"locations": [{"line": 2, "column": 14}, {"line": 3, "column": 20}],
3637
"path": path,
3738
"extensions": extensions,
3839
}

tests/execution/test_abstract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def resolve_type_on_interface_yields_useful_error():
254254
assert format_error(result.errors[0]) == {
255255
"message": "Runtime Object type 'Human'"
256256
" is not a possible type for 'Pet'.",
257-
"locations": [(3, 15)],
257+
"locations": [{"line": 3, "column": 15}],
258258
"path": ["pets", 2],
259259
}
260260

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

tests/execution/test_abstract_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ async def is_type_of_with_async_error():
182182
assert list(map(format_error, result.errors)) == [ # type: ignore
183183
{
184184
"message": "We are testing this error",
185-
"locations": [(3, 15)],
185+
"locations": [{"line": 3, "column": 15}],
186186
"path": ["pets", 0],
187187
},
188188
{
189189
"message": "We are testing this error",
190-
"locations": [(3, 15)],
190+
"locations": [{"line": 3, "column": 15}],
191191
"path": ["pets", 1],
192192
},
193193
]
@@ -334,7 +334,7 @@ async def resolve_type_on_interface_yields_useful_error():
334334
assert format_error(result.errors[0]) == {
335335
"message": "Runtime Object type 'Human'"
336336
" is not a possible type for 'Pet'.",
337-
"locations": [(3, 15)],
337+
"locations": [{"line": 3, "column": 15}],
338338
"path": ["pets", 2],
339339
}
340340

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

tests/language/test_location.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from graphql import SourceLocation
2+
3+
4+
def describe_SourceLocation():
5+
def equals_with_itself():
6+
assert SourceLocation(1, 2) == SourceLocation(1, 2)
7+
assert (SourceLocation(1, 2) != SourceLocation(1, 2)) is False
8+
9+
def equals_with_formatted_form():
10+
sl = SourceLocation(1, 2)
11+
assert SourceLocation(1, 2) == sl.formatted
12+
assert (SourceLocation(1, 2) != sl.formatted) is False

0 commit comments

Comments
 (0)