Skip to content

Commit

Permalink
Merge pull request #62 from fintoc-com/fix/fix-http-error-decorator
Browse files Browse the repository at this point in the history
Fix http error decorator
  • Loading branch information
vnazar authored Oct 7, 2024
2 parents 9661e14 + 4c3041d commit 3f79cf7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
12 changes: 6 additions & 6 deletions fintoc/mixins/manager_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABCMeta, abstractmethod

from fintoc.resource_handlers import resource_all, resource_create, resource_get
from fintoc.utils import can_raise_http_error, get_resource_class
from fintoc.utils import can_raise_fintoc_error, get_resource_class


class ManagerMixin(metaclass=ABCMeta): # pylint: disable=no-self-use
Expand Down Expand Up @@ -44,7 +44,7 @@ def methods(self):
one of: ['all', 'get', 'create', 'update', 'delete'].
"""

@can_raise_http_error
@can_raise_fintoc_error
def _all(self, **kwargs):
"""
Return all instances of the resource being handled by the manager.
Expand All @@ -61,7 +61,7 @@ def _all(self, **kwargs):
)
return self.post_all_handler(objects, **kwargs)

@can_raise_http_error
@can_raise_fintoc_error
def _get(self, identifier, **kwargs):
"""
Return an instance of the resource being handled by the manager,
Expand All @@ -79,7 +79,7 @@ def _get(self, identifier, **kwargs):
)
return self.post_get_handler(object_, identifier, **kwargs)

@can_raise_http_error
@can_raise_fintoc_error
def _create(self, **kwargs):
"""
Create an instance of the resource being handled by the manager.
Expand All @@ -96,7 +96,7 @@ def _create(self, **kwargs):
)
return self.post_create_handler(object_, **kwargs)

@can_raise_http_error
@can_raise_fintoc_error
def _update(self, identifier, **kwargs):
"""
Update an instance of the resource being handled by the manager,
Expand All @@ -106,7 +106,7 @@ def _update(self, identifier, **kwargs):
object_ = self._get(identifier)
return object_.update(**kwargs)

@can_raise_http_error
@can_raise_fintoc_error
def _delete(self, identifier, **kwargs):
"""
Delete an instance of the resource being handled by the manager,
Expand Down
6 changes: 3 additions & 3 deletions fintoc/mixins/resource_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fintoc.resource_handlers import resource_delete, resource_update
from fintoc.utils import (
can_raise_http_error,
can_raise_fintoc_error,
get_resource_class,
objetize,
serialize,
Expand Down Expand Up @@ -60,7 +60,7 @@ def serialize(self):
serialized = {**serialized, key: element}
return serialized

@can_raise_http_error
@can_raise_fintoc_error
def _update(self, **kwargs):
id_ = getattr(self, self.__class__.resource_identifier)
object_ = resource_update(
Expand All @@ -76,7 +76,7 @@ def _update(self, **kwargs):
self.__dict__.update(object_.__dict__)
return self

@can_raise_http_error
@can_raise_fintoc_error
def _delete(self, **kwargs):
identifier = getattr(self, self.__class__.resource_identifier)
resource_delete(
Expand Down
6 changes: 3 additions & 3 deletions fintoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ def get_error_class(snake_error_name):
return getattr(module, snake_to_pascal(snake_error_name), FintocError)


def can_raise_http_error(function):
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
34 changes: 25 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from fintoc.errors import ApiError, FintocError
from fintoc.resources import GenericFintocResource, Link
from fintoc.utils import (
can_raise_http_error,
can_raise_fintoc_error,
get_error_class,
get_resource_class,
is_iso_datetime,
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_invalid_error(self):
assert error is FintocError


class TestCanRaiseHTTPError:
class TestCanRaiseFintocError:
@pytest.fixture(autouse=True)
def patch_http_error(self, patch_http_error):
pass
Expand All @@ -126,28 +126,44 @@ def setup_method(self):
def no_error():
pass

def raise_http_error():
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_error = raise_http_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):
wrapped = can_raise_http_error(self.no_error)
wrapped = can_raise_fintoc_error(self.no_error)
wrapped()

def test_http_error(self):
wrapped = can_raise_http_error(self.raise_http_error)
def test_http_status_error(self):
wrapped = can_raise_fintoc_error(self.raise_http_status_error)
with pytest.raises(Exception) as execinfo:
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_http_error(self.raise_generic_error)
wrapped = can_raise_fintoc_error(self.raise_generic_error)
with pytest.raises(Exception) as execinfo:
wrapped()
assert not isinstance(execinfo.value, FintocError)
Expand Down

0 comments on commit 3f79cf7

Please sign in to comment.