From da08390124f317a0167a2213b07fcfa87a1d12dd Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sat, 10 Sep 2022 03:18:46 +0200 Subject: [PATCH 1/2] Fix error string if errors field contain a dict --- gql/client.py | 10 ++++++---- gql/utils.py | 11 ++++++++++- tests/test_aiohttp.py | 13 ++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/gql/client.py b/gql/client.py index 6a6f5a67..8c62c45c 100644 --- a/gql/client.py +++ b/gql/client.py @@ -34,6 +34,7 @@ from .utilities import build_client_schema from .utilities import parse_result as parse_result_fn from .utilities import serialize_variable_values +from .utils import str_first_element """ Load the appropriate instance of the Literal type @@ -152,7 +153,8 @@ def _build_schema_from_introspection(self, execution_result: ExecutionResult): if execution_result.errors: raise TransportQueryError( ( - f"Error while fetching schema: {execution_result.errors[0]!s}\n" + "Error while fetching schema: " + f"{str_first_element(execution_result.errors)!s}\n" "If you don't need the schema, you can try with: " '"fetch_schema_from_transport=False"' ), @@ -858,7 +860,7 @@ def execute( # Raise an error if an error is returned in the ExecutionResult object if result.errors: raise TransportQueryError( - str(result.errors[0]), + str_first_element(result.errors), errors=result.errors, data=result.data, extensions=result.extensions, @@ -1066,7 +1068,7 @@ async def subscribe( # Raise an error if an error is returned in the ExecutionResult object if result.errors: raise TransportQueryError( - str(result.errors[0]), + str_first_element(result.errors), errors=result.errors, data=result.data, extensions=result.extensions, @@ -1229,7 +1231,7 @@ async def execute( # Raise an error if an error is returned in the ExecutionResult object if result.errors: raise TransportQueryError( - str(result.errors[0]), + str_first_element(result.errors), errors=result.errors, data=result.data, extensions=result.extensions, diff --git a/gql/utils.py b/gql/utils.py index 3edb086c..b4265ce1 100644 --- a/gql/utils.py +++ b/gql/utils.py @@ -1,6 +1,6 @@ """Utilities to manipulate several python objects.""" -from typing import Any, Dict, Tuple, Type +from typing import Any, Dict, List, Tuple, Type # From this response in Stackoverflow @@ -47,3 +47,12 @@ def recurse_extract(path, obj): nulled_variables = recurse_extract("variables", variables) return nulled_variables, files + + +def str_first_element(errors: List) -> str: + try: + first_error = errors[0] + except (KeyError, TypeError): + first_error = errors + + return str(first_error) diff --git a/tests/test_aiohttp.py b/tests/test_aiohttp.py index 3a84d21e..f1a3cdf5 100644 --- a/tests/test_aiohttp.py +++ b/tests/test_aiohttp.py @@ -199,18 +199,21 @@ async def handler(request): assert "500, message='Internal Server Error'" in str(exc_info.value) -query1_server_error_answer = '{"errors": ["Error 1", "Error 2"]}' +transport_query_error_responses = [ + '{"errors": ["Error 1", "Error 2"]}', + '{"errors": {"error_1": "Something"}}', + '{"errors": 5}', +] @pytest.mark.asyncio -async def test_aiohttp_error_code(event_loop, aiohttp_server): +@pytest.mark.parametrize("query_error", transport_query_error_responses) +async def test_aiohttp_error_code(event_loop, aiohttp_server, query_error): from aiohttp import web from gql.transport.aiohttp import AIOHTTPTransport async def handler(request): - return web.Response( - text=query1_server_error_answer, content_type="application/json" - ) + return web.Response(text=query_error, content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) From 4e16cfbbd844778bbcc99a3ba70432aea84850b5 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sat, 10 Sep 2022 03:21:10 +0200 Subject: [PATCH 2/2] removing redundant !s --- gql/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gql/client.py b/gql/client.py index 8c62c45c..69804faa 100644 --- a/gql/client.py +++ b/gql/client.py @@ -154,7 +154,7 @@ def _build_schema_from_introspection(self, execution_result: ExecutionResult): raise TransportQueryError( ( "Error while fetching schema: " - f"{str_first_element(execution_result.errors)!s}\n" + f"{str_first_element(execution_result.errors)}\n" "If you don't need the schema, you can try with: " '"fetch_schema_from_transport=False"' ),