libb.ErrorCatcher

class ErrorCatcher(name, bases, dct)[source]

Bases: type

Metaclass that wraps all methods with exception catching.

Automatically applies exception handling to all callable attributes of a class, preventing exceptions from propagating. Can optionally specify the logging level for all wrapped methods via the _error_log_level class attribute.

Automatic Exception Handling:

>>> import logging
>>> logging.getLogger(__name__).setLevel(logging.CRITICAL)
>>> class Test(metaclass=ErrorCatcher):
...     def __init__(self, val):
...         self.val = val
...     def calc(self):
...         return self.val / 0
>>> t = Test(5)
>>> t.calc() is None
True

With Custom Log Level:

>>> class TestWithLevel(metaclass=ErrorCatcher):
...     _error_log_level = logging.ERROR
...     def risky(self):
...         raise RuntimeError("Oops")
>>> t2 = TestWithLevel()
>>> t2.risky() is None
True