Skip to content

Commit da08390

Browse files
committed
Fix error string if errors field contain a dict
1 parent 5a9c0cc commit da08390

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

gql/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from .utilities import build_client_schema
3535
from .utilities import parse_result as parse_result_fn
3636
from .utilities import serialize_variable_values
37+
from .utils import str_first_element
3738

3839
"""
3940
Load the appropriate instance of the Literal type
@@ -152,7 +153,8 @@ def _build_schema_from_introspection(self, execution_result: ExecutionResult):
152153
if execution_result.errors:
153154
raise TransportQueryError(
154155
(
155-
f"Error while fetching schema: {execution_result.errors[0]!s}\n"
156+
"Error while fetching schema: "
157+
f"{str_first_element(execution_result.errors)!s}\n"
156158
"If you don't need the schema, you can try with: "
157159
'"fetch_schema_from_transport=False"'
158160
),
@@ -858,7 +860,7 @@ def execute(
858860
# Raise an error if an error is returned in the ExecutionResult object
859861
if result.errors:
860862
raise TransportQueryError(
861-
str(result.errors[0]),
863+
str_first_element(result.errors),
862864
errors=result.errors,
863865
data=result.data,
864866
extensions=result.extensions,
@@ -1066,7 +1068,7 @@ async def subscribe(
10661068
# Raise an error if an error is returned in the ExecutionResult object
10671069
if result.errors:
10681070
raise TransportQueryError(
1069-
str(result.errors[0]),
1071+
str_first_element(result.errors),
10701072
errors=result.errors,
10711073
data=result.data,
10721074
extensions=result.extensions,
@@ -1229,7 +1231,7 @@ async def execute(
12291231
# Raise an error if an error is returned in the ExecutionResult object
12301232
if result.errors:
12311233
raise TransportQueryError(
1232-
str(result.errors[0]),
1234+
str_first_element(result.errors),
12331235
errors=result.errors,
12341236
data=result.data,
12351237
extensions=result.extensions,

gql/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Utilities to manipulate several python objects."""
22

3-
from typing import Any, Dict, Tuple, Type
3+
from typing import Any, Dict, List, Tuple, Type
44

55

66
# From this response in Stackoverflow
@@ -47,3 +47,12 @@ def recurse_extract(path, obj):
4747
nulled_variables = recurse_extract("variables", variables)
4848

4949
return nulled_variables, files
50+
51+
52+
def str_first_element(errors: List) -> str:
53+
try:
54+
first_error = errors[0]
55+
except (KeyError, TypeError):
56+
first_error = errors
57+
58+
return str(first_error)

tests/test_aiohttp.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,21 @@ async def handler(request):
199199
assert "500, message='Internal Server Error'" in str(exc_info.value)
200200

201201

202-
query1_server_error_answer = '{"errors": ["Error 1", "Error 2"]}'
202+
transport_query_error_responses = [
203+
'{"errors": ["Error 1", "Error 2"]}',
204+
'{"errors": {"error_1": "Something"}}',
205+
'{"errors": 5}',
206+
]
203207

204208

205209
@pytest.mark.asyncio
206-
async def test_aiohttp_error_code(event_loop, aiohttp_server):
210+
@pytest.mark.parametrize("query_error", transport_query_error_responses)
211+
async def test_aiohttp_error_code(event_loop, aiohttp_server, query_error):
207212
from aiohttp import web
208213
from gql.transport.aiohttp import AIOHTTPTransport
209214

210215
async def handler(request):
211-
return web.Response(
212-
text=query1_server_error_answer, content_type="application/json"
213-
)
216+
return web.Response(text=query_error, content_type="application/json")
214217

215218
app = web.Application()
216219
app.router.add_route("POST", "/", handler)

0 commit comments

Comments
 (0)