Skip to content

Commit

Permalink
Raise original exception instead of RetryError in logs (#99)
Browse files Browse the repository at this point in the history
* Raise original exception instead of RetryError in logs

* Update sw_utils/decorators.py

Co-authored-by: evgeny-stakewise <[email protected]>
Signed-off-by: antares-sw <[email protected]>

* fix

* Fix lint

---------

Signed-off-by: antares-sw <[email protected]>
Co-authored-by: evgeny-stakewise <[email protected]>
  • Loading branch information
antares-sw and evgeny-stakewise authored Apr 29, 2024
1 parent 522acb0 commit 67a5985
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
.idea
local.env
*.env
*.DS_Store
21 changes: 18 additions & 3 deletions sw_utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
from functools import wraps
from typing import Any, Callable, Optional
from typing import Any, Callable, NoReturn, Optional

import aiohttp
from tenacity import (
Expand Down Expand Up @@ -50,6 +50,11 @@ def default_log_before(retry_state: 'RetryCallState') -> None:
default_logger.log(logging.INFO, msg, *args)


def default_after(future: Any) -> NoReturn:
"""Reraises the original exception from the Future, suppressing 'RetryError' messages."""
raise future.outcome.exception()


def can_be_retried_aiohttp_error(e: BaseException) -> bool:
if isinstance(e, (asyncio.TimeoutError, aiohttp.ClientConnectionError)):
return True
Expand All @@ -60,19 +65,29 @@ def can_be_retried_aiohttp_error(e: BaseException) -> bool:
return False


def retry_aiohttp_errors(delay: int = 60, before: Optional[Callable] = None) -> Any:
def retry_aiohttp_errors(
delay: int = 60,
before: Optional[Callable] = None,
after: Optional[Callable] = None,
) -> Any:
return retry(
retry=retry_if_exception(can_be_retried_aiohttp_error),
wait=wait_exponential(multiplier=1, min=1, max=delay // 2),
stop=stop_after_delay(delay),
before=before or default_log_before,
after=after or default_after,
)


def retry_ipfs_exception(delay: int, before: Optional[Callable] = None) -> Any:
def retry_ipfs_exception(
delay: int = 60,
before: Optional[Callable] = None,
after: Optional[Callable] = None,
) -> Any:
return retry(
retry=retry_if_exception_type(IpfsException),
wait=wait_exponential(multiplier=1, min=1, max=delay // 2),
stop=stop_after_delay(delay),
before=before or default_log_before,
after=after or default_after,
)

0 comments on commit 67a5985

Please sign in to comment.