Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix APIErrorCode class (did not work as expected) #191

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Checkout pytest fixtures for another way to do this: https://docs.pytest.org/en/6.2.x/fixture.html#parametrizing-fixtures

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep yep! We use them in other tests in the notion-qms repo I think. If we expand this module I'll probably end up shifting to that method to avoid definition repetition.

("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
Loading