Skip to content

Commit

Permalink
better error handling (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
wakamex authored May 13, 2024
1 parent ad25ef2 commit 4ef9d1b
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions ape_alchemy/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def __init__(self, *args, **kwargs):
alchemy_config = cast(AlchemyConfig, self.config_manager.get_config("alchemy"))
self.concurrency = alchemy_config.concurrency
self.block_page_size = alchemy_config.block_page_size
# overwrite for testing
self.block_page_size = 5000
self.concurrency = 1

@property
def uri(self):
Expand Down Expand Up @@ -248,7 +251,22 @@ def _make_request(
try:
return super()._make_request(endpoint, parameters)
except HTTPError as err:
message = str(err)
# safely get response date
response_data = err.response.json() if err.response else {}

# check if we have an error message, otherwise throw an error
if "error" not in response_data:
raise AlchemyProviderError(str(err)) from err

# safely get error message
error_data = response_data["error"]
message = (
error_data.get("message", str(error_data))
if isinstance(error_data, dict)
else error_data
)

# handle known error messages and continue
if any(
error in message
for error in ["exceeded its compute units", "Too Many Requests for url"]
Expand All @@ -265,8 +283,8 @@ def _make_request(
delay = retry_interval + random.randint(0, retry_jitter)
time.sleep(delay / 1000)
continue
elif "error" not in message:
raise AlchemyProviderError(str(err)) from err

# freak out if we get here
cls = (
AlchemyFeatureNotAvailable
if "is not available" in message
Expand Down

0 comments on commit 4ef9d1b

Please sign in to comment.