From d9353b159999ab4fcb74af17481eac22fe37d88a Mon Sep 17 00:00:00 2001 From: Anton Shutik Date: Fri, 13 Dec 2024 16:26:03 +0100 Subject: [PATCH 1/2] Added GraphQL error handling --- shopify_client/exceptions.py | 2 ++ shopify_client/graphql.py | 9 +++++- tests/test_graphql.py | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 shopify_client/exceptions.py diff --git a/shopify_client/exceptions.py b/shopify_client/exceptions.py new file mode 100644 index 0000000..5a81969 --- /dev/null +++ b/shopify_client/exceptions.py @@ -0,0 +1,2 @@ +class GraphQLError(Exception): + pass diff --git a/shopify_client/graphql.py b/shopify_client/graphql.py index 4359425..2c676bb 100644 --- a/shopify_client/graphql.py +++ b/shopify_client/graphql.py @@ -4,6 +4,8 @@ import requests +from .exceptions import GraphQLError + logger = logging.getLogger(__name__) @@ -56,7 +58,12 @@ def __query( "operationName": operation_name, }, ) - return self.client.parse_response(response) + parsed_response = self.client.parse_response(response) + if "errors" in parsed_response: + logger.error(f"GraphQL errors: {parsed_response['errors']}") + if parsed_response.get("data", None) is None: + raise GraphQLError(f"GraphQL errors: {parsed_response['errors']}") + return parsed_response except requests.exceptions.HTTPError as e: logger.warning(f"Failed to execute GraphQL query: {repr(e)}") raise e diff --git a/tests/test_graphql.py b/tests/test_graphql.py index 7d7efb9..e52adcb 100644 --- a/tests/test_graphql.py +++ b/tests/test_graphql.py @@ -2,6 +2,7 @@ from unittest.mock import call, mock_open, patch import requests import pytest +from shopify_client.exceptions import GraphQLError from shopify_client.graphql import GraphQL from tests.conftest import CopyingMock @@ -159,3 +160,60 @@ def test_paginated_query_requires_has_next_page(graphql, mock_client): def test_paginated_query_requires_end_cursor(graphql, mock_client): with pytest.raises(AssertionError): list(graphql(query="query { pageInfo { hasNextPage } }", paginate=True)) + + +def test_graphql_query_handles_empty_data_with_errors_only(graphql, mock_client): + mock_client.post.return_value = {"errors": [{"message": "Another error occurred"}]} + with pytest.raises(GraphQLError, match='[{"message": "Another error occurred"}]'): + graphql(query="query { key }") + + +def test_graphql_query_reraises_http_error(graphql, mock_client): + mock_client.post.side_effect = requests.exceptions.HTTPError("HTTP Error") + with pytest.raises(requests.exceptions.HTTPError): + graphql(query="query { key }") + + +def test_graphql_query_reraises_json_error(graphql, mock_client): + mock_client.post.side_effect = json.JSONDecodeError("JSON Decode Error", "", 0) + with pytest.raises(json.JSONDecodeError): + graphql(query="query { key }") + + +def test_graphql_query_handles_data_with_errors(graphql, mock_client): + mock_client.post.return_value = { + "data": {"key": "value"}, + "errors": [{"message": "Some error occurred"}], + } + + # Mock the logger from the client + with patch("shopify_client.graphql.logger") as mock_logger: + response = graphql(query="query { key }") + + # Check that the response contains the data + assert response == { + "data": {"key": "value"}, + "errors": [{"message": "Some error occurred"}], + } + + # Verify that the logger was called with the expected message + mock_logger.error.assert_called_once_with( + "GraphQL errors: [{'message': 'Some error occurred'}]" + ) + + +def test_graphql_query_handles_none_data_with_errors(graphql, mock_client): + mock_client.post.return_value = { + "data": None, + "errors": [{"message": "Some error occurred"}], + } + + # Mock the logger from the client + with patch("shopify_client.graphql.logger") as mock_logger: + with pytest.raises(GraphQLError, match='[{"message": "Some error occurred"}]'): + graphql(query="query { key }") + + # Verify that the logger was called with the expected message + mock_logger.error.assert_called_once_with( + "GraphQL errors: [{'message': 'Some error occurred'}]" + ) From 6dbe6e77b4f567917ad653b9113ffc265bf6477e Mon Sep 17 00:00:00 2001 From: Anton Shutik Date: Fri, 13 Dec 2024 16:30:03 +0100 Subject: [PATCH 2/2] v0.0.8 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 5a5831a..d169b2f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.7 +0.0.8