diff --git a/gql/transport/aiohttp.py b/gql/transport/aiohttp.py index f17d3f5b..cd5b44a0 100644 --- a/gql/transport/aiohttp.py +++ b/gql/transport/aiohttp.py @@ -204,12 +204,20 @@ async def execute( resp.raise_for_status() except ClientResponseError as e: - raise TransportServerError from e + raise TransportServerError(str(e)) from e - raise TransportProtocolError("Server did not return a GraphQL result") + result_text = await resp.text() + raise TransportProtocolError( + f"Server did not return a GraphQL result: {result_text}" + ) if "errors" not in result and "data" not in result: - raise TransportProtocolError("Server did not return a GraphQL result") + result_text = await resp.text() + raise TransportProtocolError( + "Server did not return a GraphQL result: " + 'No "data" or "error" keys in answer: ' + f"{result_text}" + ) return ExecutionResult(errors=result.get("errors"), data=result.get("data")) diff --git a/gql/transport/websockets.py b/gql/transport/websockets.py index 63af4703..76a234bd 100644 --- a/gql/transport/websockets.py +++ b/gql/transport/websockets.py @@ -325,7 +325,7 @@ def _parse_answer( except ValueError as e: raise TransportProtocolError( - "Server did not return a GraphQL result" + f"Server did not return a GraphQL result: {answer}" ) from e return answer_type, answer_id, execution_result diff --git a/tests/test_aiohttp.py b/tests/test_aiohttp.py index 1a0d3af0..382c4fa8 100644 --- a/tests/test_aiohttp.py +++ b/tests/test_aiohttp.py @@ -83,9 +83,11 @@ async def handler(request): query = gql(query1_str) - with pytest.raises(TransportServerError): + with pytest.raises(TransportServerError) as exc_info: await session.execute(query) + assert "500, message='Internal Server Error'" in str(exc_info.value) + query1_server_error_answer = '{"errors": ["Error 1", "Error 2"]}' @@ -114,15 +116,34 @@ async def handler(request): invalid_protocol_responses = [ - "{}", - "qlsjfqsdlkj", - '{"not_data_or_errors": 35}', + { + "response": "{}", + "expected_exception": ( + "Server did not return a GraphQL result: " + 'No "data" or "error" keys in answer: {}' + ), + }, + { + "response": "qlsjfqsdlkj", + "expected_exception": ( + "Server did not return a GraphQL result: " "qlsjfqsdlkj" + ), + }, + { + "response": '{"not_data_or_errors": 35}', + "expected_exception": ( + "Server did not return a GraphQL result: " + 'No "data" or "error" keys in answer: {"not_data_or_errors": 35}' + ), + }, ] @pytest.mark.asyncio -@pytest.mark.parametrize("response", invalid_protocol_responses) -async def test_aiohttp_invalid_protocol(event_loop, aiohttp_server, response): +@pytest.mark.parametrize("param", invalid_protocol_responses) +async def test_aiohttp_invalid_protocol(event_loop, aiohttp_server, param): + response = param["response"] + async def handler(request): return web.Response(text=response, content_type="application/json") @@ -138,9 +159,11 @@ async def handler(request): query = gql(query1_str) - with pytest.raises(TransportProtocolError): + with pytest.raises(TransportProtocolError) as exc_info: await session.execute(query) + assert param["expected_exception"] in str(exc_info.value) + @pytest.mark.asyncio async def test_aiohttp_subscribe_not_supported(event_loop, aiohttp_server):