From d53254361c6bfa343b26e1ef6155e37de2117f64 Mon Sep 17 00:00:00 2001 From: BDeeming Date: Mon, 7 Nov 2022 09:39:13 +0000 Subject: [PATCH] Sink Exceptions to Nothing values --- pymaybe/__init__.py | 5 ++++- tests/test_pymaybe.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pymaybe/__init__.py b/pymaybe/__init__.py index d98a919..b047338 100755 --- a/pymaybe/__init__.py +++ b/pymaybe/__init__.py @@ -123,7 +123,10 @@ def __init__(self, value): self.__value = value def __call__(self, *args, **kwargs): - return maybe(self.__value(*args, **kwargs)) + try: + return maybe(self.__value(*args, **kwargs)) + except Exception: + return Nothing() # region Comparison def __cmp__(self, other): diff --git a/tests/test_pymaybe.py b/tests/test_pymaybe.py index 37d7809..d997d76 100755 --- a/tests/test_pymaybe.py +++ b/tests/test_pymaybe.py @@ -367,6 +367,13 @@ def test_something_forwardsMethodCalls_handlesNonExisting(self): result = maybe('VALUE').lowerr() assert result.is_none() + def test_something_forwardsMethodCalls_handlesException(self): + class Thrower(): + def throw(self): + raise Exception() + result = maybe(Thrower()).throw() + assert result.is_none() + def test_nothing_forwardsMethodCalls_handlesNonExisting(self): result = maybe(None).invalid().call() assert result.is_none()