Skip to content

Commit b78d6ac

Browse files
committed
Mark raise_for_error as deprecated
1 parent 003fb3a commit b78d6ac

File tree

2 files changed

+20
-41
lines changed

2 files changed

+20
-41
lines changed

infrahub_sdk/client.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import copy
55
import logging
66
import time
7+
import warnings
78
from collections.abc import Coroutine, MutableMapping
89
from functools import wraps
910
from time import sleep
@@ -893,13 +894,20 @@ async def execute_graphql(
893894
branch_name (str, optional): Name of the branch on which the query will be executed. Defaults to None.
894895
at (str, optional): Time when the query should be executed. Defaults to None.
895896
timeout (int, optional): Timeout in second for the query. Defaults to None.
896-
raise_for_error (bool, optional): Flag to indicate that we need to raise an exception if the response has some errors. Defaults to True.
897+
raise_for_error (bool, optional): Deprecated, flag to indicate that we need to raise an exception if the response has some errors.
898+
Defaults to True.
897899
Raises:
898900
GraphQLError: _description_
899901
900902
Returns:
901903
_type_: _description_
902904
"""
905+
if not raise_for_error:
906+
warnings.warn(
907+
"Setting `raise_for_error=False` is deprecated, use `try/except` to handle errors.",
908+
DeprecationWarning,
909+
stacklevel=1,
910+
)
903911

904912
branch_name = branch_name or self.default_branch
905913
url = self._graphql_url(branch_name=branch_name, at=at)
@@ -950,9 +958,7 @@ async def execute_graphql(
950958
response = decode_json(response=resp)
951959

952960
if "errors" in response:
953-
if raise_for_error:
954-
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
955-
return response["errors"]
961+
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
956962

957963
return response["data"]
958964

@@ -1639,13 +1645,20 @@ def execute_graphql(
16391645
branch_name (str, optional): Name of the branch on which the query will be executed. Defaults to None.
16401646
at (str, optional): Time when the query should be executed. Defaults to None.
16411647
timeout (int, optional): Timeout in second for the query. Defaults to None.
1642-
raise_for_error (bool, optional): Flag to indicate that we need to raise an exception if the response has some errors. Defaults to True.
1648+
raise_for_error (bool, optional): Deprecated, flag to indicate that we need to raise an exception if the response has some errors.
1649+
Defaults to True.
16431650
Raises:
16441651
GraphQLError: When an error occurs during the execution of the GraphQL query or mutation.
16451652
16461653
Returns:
16471654
dict: The result of the GraphQL query or mutation.
16481655
"""
1656+
if not raise_for_error:
1657+
warnings.warn(
1658+
"Setting `raise_for_error=False` is deprecated, use `try/except` to handle errors.",
1659+
DeprecationWarning,
1660+
stacklevel=1,
1661+
)
16491662

16501663
branch_name = branch_name or self.default_branch
16511664
url = self._graphql_url(branch_name=branch_name, at=at)
@@ -1696,9 +1709,7 @@ def execute_graphql(
16961709
response = decode_json(response=resp)
16971710

16981711
if "errors" in response:
1699-
if raise_for_error:
1700-
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
1701-
return response["errors"]
1712+
raise GraphQLError(errors=response["errors"], query=query, variables=variables)
17021713

17031714
return response["data"]
17041715

tests/unit/sdk/test_client.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pytest_httpx import HTTPXMock
55

66
from infrahub_sdk import InfrahubClient, InfrahubClientSync
7-
from infrahub_sdk.exceptions import GraphQLError, NodeNotFoundError
7+
from infrahub_sdk.exceptions import NodeNotFoundError
88
from infrahub_sdk.node import InfrahubNode, InfrahubNodeSync
99
from tests.unit.sdk.conftest import BothClients
1010

@@ -800,35 +800,3 @@ async def test_clone_define_branch(clients: BothClients, client_type: str) -> No
800800
assert clone.default_branch == clone_branch
801801
assert original_branch != clone_branch
802802
assert clone.store._default_branch == clone_branch
803-
804-
805-
@pytest.mark.parametrize("client_type", client_types)
806-
async def test_execute_graphql_error(httpx_mock: HTTPXMock, clients, client_type) -> None:
807-
httpx_mock.add_response(method="POST", json={"errors": ["foo"]}, is_reusable=True)
808-
809-
query = """
810-
query GetTags {
811-
BuiltinTag {
812-
edges {
813-
node {
814-
id
815-
display_label
816-
}
817-
}
818-
}
819-
}
820-
"""
821-
822-
if client_type == "standard":
823-
with pytest.raises(GraphQLError):
824-
await clients.standard.execute_graphql(query=query, raise_for_error=True)
825-
826-
response = await clients.standard.execute_graphql(query=query, raise_for_error=False)
827-
else:
828-
with pytest.raises(GraphQLError):
829-
clients.sync.execute_graphql(query=query, raise_for_error=True)
830-
831-
response = clients.sync.execute_graphql(query=query, raise_for_error=False)
832-
833-
assert response
834-
assert response[0] == "foo"

0 commit comments

Comments
 (0)