Skip to content
Snippets Groups Projects
Commit 653dd7e0 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

fix rethrow

parent 68aa2471
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ hs_err_pid*.log
venv
coverage.xml
.coverage.*
*__pycache__*
.metadata
test
lock
......
......@@ -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:
"""
......
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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment