Skip to content

Commit

Permalink
Ignore exp get return on error (#30)
Browse files Browse the repository at this point in the history
* ignore_exceptions: support use return value on error

* ignore_exceptions: support use return value on error

* ignore_exceptions: support use return value on error

* ignore_exceptions: support use return value on error

* ignore_exceptions: support use return value on error

* ignore_exceptions: support use return value on error

* ignore_exceptions: support use return value on error
  • Loading branch information
myakove authored Apr 14, 2024
1 parent b5cf13b commit 791c22b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pyhelper_utils/general.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations
import re
from time import sleep
from functools import wraps
from logging import Logger
from typing import Any, Optional
from typing import Any


def tts(ts: Any) -> int:
Expand Down Expand Up @@ -39,14 +40,21 @@ def tts(ts: Any) -> int:
return int(ts)


def ignore_exceptions(logger: Optional[Logger] = None, retry: int = 0, retry_interval: int = 1) -> Any:
def ignore_exceptions(
retry: int = 0,
retry_interval: int = 1,
return_on_error: Any = None,
logger: Logger | None = None,
) -> Any:
"""
Decorator to ignore exceptions with support for retry.
Args:
logger (Logger): logger to use, if not passed no logs will be displayed.
retry (int): Number of retry if the underline function throw exception.
retry_interval (int): Number of seconds to wait between retries.
return_on_error (Any): Return value if the underline function throw exception.
logger (Logger): logger to use, if not passed no logs will be displayed.
Returns:
any: the underline function return value.
Expand All @@ -68,7 +76,7 @@ def inner(*args, **kwargs):

if logger:
logger.info(f"{func.__name__} error: {ex}")
return None
return return_on_error

return inner

Expand Down
16 changes: 15 additions & 1 deletion tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@

@pytest.fixture
def func_for_ignore_exception():
@ignore_exceptions(logger=logging.getLogger(), retry=1)
@ignore_exceptions(logger=logging.getLogger(), retry=1, retry_interval=1)
def _foo():
raise ValueError()

return _foo


@pytest.fixture
def func_for_ignore_exception_with_return_value_on_error():
@ignore_exceptions(logger=logging.getLogger(), retry_interval=1, return_on_error="test")
def _foo():
raise ValueError()

Expand All @@ -20,3 +29,8 @@ def test_tts():

def test_ignore_exceptions(func_for_ignore_exception):
assert not isinstance(func_for_ignore_exception(), Exception)
assert not func_for_ignore_exception()


def test_ignore_exceptions_with_return_value(func_for_ignore_exception_with_return_value_on_error):
assert func_for_ignore_exception_with_return_value_on_error() == "test"

0 comments on commit 791c22b

Please sign in to comment.