diff --git a/.gitignore b/.gitignore index 49bad4a9adcf19af864a6aa517c80ff8317c99d8..c3176ae640c8bad9067a8456d3e85627f3db4d19 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ hs_err_pid*.log venv coverage.xml .coverage.* +*__pycache__* .metadata test lock diff --git a/satella/coding/recast_exceptions.py b/satella/coding/recast_exceptions.py index 738ced9fe63ea26df01470d9b3c73e9fb082278a..6ab8ff6a75c55277089f125a0d7e990570bf97db 100644 --- a/satella/coding/recast_exceptions.py +++ b/satella/coding/recast_exceptions.py @@ -86,9 +86,15 @@ class log_exceptions: """Return whether the exception has been logged""" if not isinstance(e, self.exc_types): return False - format_dict = {'args': args, 'kwargs': kwargs} + format_dict = {} + if '{args}' in self.format_string: + format_dict['args'] = args + if '{kwargs}' in self.format_string: + format_dict['kwargs'] = kwargs if self.locals is not None: - format_dict.update(self.locals) + for key, value in self.locals.items(): + if '{%s}' % (key,) in self.format_string: + format_dict[key] = value format_dict['e'] = e self.logger.log(self.severity, self.format_string.format(**format_dict), exc_info=e) @@ -217,6 +223,8 @@ class rethrow_as: However, during to richer set of switches and capability to return a value this is not deprecated. + .. deprecated:: v + :param exception_preprocessor: other callable/1 to use instead of repr. Should return a str, a text description of the exception :param returns: what value should the function return if this is used as a decorator @@ -283,6 +291,8 @@ class rethrow_as: raise to(self.exception_preprocessor(exc_val)) + + def raises_exception(exc_class: tp.Union[ExceptionClassType, tp.Tuple[ExceptionClassType, ...]], clb: NoArgCallable[None]) -> bool: """ diff --git a/tests/test_coding/test_rethrow.py b/tests/test_coding/test_rethrow.py index 6c8dd09e178c965d57adc748dcfd1fa8f96e578f..6595e40040750ac40901ad3f7c1d258be4628658 100644 --- a/tests/test_coding/test_rethrow.py +++ b/tests/test_coding/test_rethrow.py @@ -1,9 +1,8 @@ import logging -import sys import unittest from satella.coding import rethrow_as, silence_excs, catch_exception, log_exceptions, \ - raises_exception, reraise_as + raises_exception logger = logging.getLogger(__name__) @@ -46,8 +45,14 @@ class TestStuff(unittest.TestCase): pass get_me_normal(KeyError) + @log_exceptions(logger, logging.CRITICAL, '{args}', exc_types=IndexError, swallow_exception=False) + def dupa2(): + dupa = [] + dupa[1] + self.assertRaises(ValueError, lambda: list(get_me(ValueError))) self.assertRaises(ValueError, lambda: get_me_normal(ValueError)) + self.assertRaises(IndexError, dupa2) def test_log_exceptions_decorator(self): @@ -139,7 +144,7 @@ class TestStuff(unittest.TestCase): def test_reraise(self): try: - with reraise_as(ValueError, NameError): + with rethrow_as(ValueError, NameError): raise ValueError() except NameError: return @@ -148,7 +153,7 @@ class TestStuff(unittest.TestCase): def test_reraise_silencer(self): - @reraise_as(ValueError, None) + @rethrow_as(ValueError, None) def lol(): raise ValueError()