Skip to content

Better transport exception messages #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions gql/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
2 changes: 1 addition & 1 deletion gql/transport/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 30 additions & 7 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}'

Expand Down Expand Up @@ -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")

Expand All @@ -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):
Expand Down