Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions gin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,16 @@

def augment_exception_message_and_reraise(exception, message):
"""Reraises `exception`, appending `message` to its string representation."""

class ExceptionProxy(type(exception)):
"""Acts as a proxy for an exception with an augmented message."""
__module__ = type(exception).__module__

def __init__(self):
pass

def __getattr__(self, attr_name):
return getattr(exception, attr_name)

def __str__(self):
return str(exception) + message

ExceptionProxy.__name__ = type(exception).__name__

proxy = ExceptionProxy()
if len(exception.args) == 1 and isinstance(exception.args[0], str):
# Exception with single string argument (most common case)
exception.args = (exception.args[0] + message)
else:
exception.args = exception.args + (message,)

if six.PY3:
ExceptionProxy.__qualname__ = type(exception).__qualname__
six.raise_from(proxy.with_traceback(exception.__traceback__), None)
six.raise_from(exception, None)
else:
six.reraise(proxy, None, sys.exc_info()[2])
six.reraise(exception, None, sys.exc_info()[2])


def _format_location(location):
Expand Down