Skip to content

Commit

Permalink
Fix APIErrorCode class (did not work as expected) (#191)
Browse files Browse the repository at this point in the history
* Fix APIErrorCode class (did not work as expected)

* Write tests

* Update readme

* Update supported python versions in setup.py
  • Loading branch information
sHermanGriffiths authored Jul 19, 2024
1 parent be4bb51 commit 2996afb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ Here are some features we're planning to add in the future:
the `ExpandingLinkToPageBlock` and not the linked page itself.
- Add the `UniqueIdProperty` class to represent database properties of the "unique_id" type
- Add the `UniqueIdPropertyValue` class to represent page property values of the "unique_id" type
- Modify the `APIErrorCode` class so that the `in` keyword can be used on it and to have two new
attributes that store error codes where the HTTP request should be retried and where the HTTP
request should not be retried.

### v0.10.2
- Have the `ConnectionThrottled` exception inherit the `HTTPResponseError` exception and update tests
Expand Down
21 changes: 18 additions & 3 deletions n2y/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import EnumMeta

try:
from enum import StrEnum
except ImportError:
Expand Down Expand Up @@ -92,15 +94,28 @@ def __init__(self, response, message) -> None:
super().__init__(response, message, APIErrorCode.ObjectNotFound)


class APIErrorCode(StrEnum):
class MetaEnum(EnumMeta):
is_retryable: bool
values: list[str] = []

@property
def RetryableCodes(self):
return [ec.value for ec in self if ec.is_retryable]

@property
def NonRetryableCodes(self):
return [ec.value for ec in self if not ec.is_retryable]

def __contains__(cls, key):
return key in cls.values


class APIErrorCode(StrEnum, metaclass=MetaEnum):
def __new__(cls, code: str, is_retryable: bool):
obj = str.__new__(cls, code)
cls.values.append(code)
obj._value_ = code
obj.is_retryable = is_retryable
cls.RetryableCodes = [ec.value for ec in cls if ec.is_retryable]
cls.NonretryableCodes = [ec.value for ec in cls if not ec.is_retryable]
return obj

BadGateway = "bad_gateway", True
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
keywords="notion documentation yaml markdown",
packages=find_packages(exclude=["tests"]),
Expand Down
36 changes: 36 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from n2y.errors import APIErrorCode


def test_apierrorcode_contains():
errors = [
("bad_gateway", True),
("conflict_error", True),
("database_connection_unavailable", True),
("gateway_timeout", True),
("internal_server_error", True),
("invalid_grant", False),
("invalid_json", False),
("invalid_request", False),
("invalid_request_url", False),
("missing_version", False),
("object_not_found", False),
("rate_limited", True),
("restricted_resource", False),
("service_unavailable", True),
("unauthorized", False),
("validation_error", False),
]
for error, is_retryable in errors:
assert error in APIErrorCode
if is_retryable:
assert (
error in APIErrorCode.RetryableCodes
and error not in APIErrorCode.NonRetryableCodes
)
else:
assert (
error in APIErrorCode.NonRetryableCodes
and error not in APIErrorCode.RetryableCodes
)
assert APIErrorCode(error).is_retryable == is_retryable
assert APIErrorCode(error).value == error

0 comments on commit 2996afb

Please sign in to comment.