Skip to content

Commit 6ff8f4d

Browse files
committed
format_error should return locations as a list of {"line", "column"}
1 parent 386ed9d commit 6ff8f4d

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-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+
[{"line": l.line, "column": l.column} for l in error.locations]
22+
if error.locations is not None
23+
else None
24+
),
2125
path=error.path,
2226
)
2327
if error.extensions:

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

0 commit comments

Comments
 (0)