Skip to content

Commit

Permalink
fix(utils): correct the catch of the HTTPError to HTTPStatusError
Browse files Browse the repository at this point in the history
  • Loading branch information
vnazar committed Oct 7, 2024
1 parent f4f6e84 commit 4c3041d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions fintoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ def get_error_class(snake_error_name):

def can_raise_fintoc_error(function):
"""
Decorator that catches HTTPError exceptions and raises custom
Decorator that catches HTTPStatusError exceptions and raises custom
Fintoc errors instead.
"""

def wrapper(*args, **kwargs):
try:
return function(*args, **kwargs)
except httpx.HTTPError as exc:
except httpx.HTTPStatusError as exc:
error_data = exc.response.json()
error = get_error_class(error_data["error"]["type"])
raise error(error_data["error"]) from None
Expand Down
18 changes: 17 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,24 @@ def setup_method(self):
def no_error():
pass

raise httpx.HTTPError("F")
def raise_http_status_error():
raise httpx.HTTPStatusError(
message="HTTP Status Error",
response=httpx.Response(
status_code=400, json={"error": {"type": "api_error"}}
),
request=httpx.Request("GET", "/"),
)

def raise_connect_error():
raise httpx.ConnectError(message="Connection Error")

def raise_generic_error():
raise ValueError("Not HTTP Error")

self.no_error = no_error
self.raise_http_status_error = raise_http_status_error
self.raise_connect_error = raise_connect_error
self.raise_generic_error = raise_generic_error

def test_no_error(self):
Expand All @@ -146,6 +156,12 @@ def test_http_status_error(self):
wrapped()
assert isinstance(execinfo.value, FintocError)

def test_connect_error(self):
wrapped = can_raise_fintoc_error(self.raise_connect_error)
with pytest.raises(Exception) as execinfo:
wrapped()
assert not isinstance(execinfo.value, FintocError)

def test_generic_error(self):
wrapped = can_raise_fintoc_error(self.raise_generic_error)
with pytest.raises(Exception) as execinfo:
Expand Down

0 comments on commit 4c3041d

Please sign in to comment.