Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testExceptionToUnicodeIOError(
exception_message, lpe_exception_message, fse_exception_message
):
# IOError is really stupid, unicode(IOError('á')) actually raises UnicodeEncodeError
# (not UnicodeDecodeError!)
assert ExceptionToUnicode(IOError(exception_message)) == exception_message
assert ExceptionToUnicode(IOError(fse_exception_message)) == exception_message
assert ExceptionToUnicode(IOError(lpe_exception_message)) == exception_message
def testExceptionToUnicodeUTF8():
assert ExceptionToUnicode(Exception("Ação".encode("utf-8"))) == "Ação"
def testPickle(exception_configuration):
try:
exception_configuration.RaiseExceptionUsingReraise()
except exception_configuration.exception_type as reraised_exception:
import cPickle
dumped_exception = cPickle.dumps(reraised_exception)
pickled_exception = cPickle.loads(dumped_exception)
assert ExceptionToUnicode(pickled_exception) == ExceptionToUnicode(
reraised_exception
)
assert ExceptionToUnicode(pickled_exception) != ""
assert ExceptionToUnicode(reraised_exception) != ""
def testExceptionToUnicodeBadEncoding(
exception_message, lpe_exception_message, fse_exception_message
):
assert (
ExceptionToUnicode(Exception(b"random \x90\xa1\xa2"))
== "random \ufffd\ufffd\ufffd"
)
def testPickle(exception_configuration):
try:
exception_configuration.RaiseExceptionUsingReraise()
except exception_configuration.exception_type as reraised_exception:
import cPickle
dumped_exception = cPickle.dumps(reraised_exception)
pickled_exception = cPickle.loads(dumped_exception)
assert ExceptionToUnicode(pickled_exception) == ExceptionToUnicode(
reraised_exception
)
assert ExceptionToUnicode(pickled_exception) != ""
assert ExceptionToUnicode(reraised_exception) != ""
def FunctionWithReraise():
from barril._foundation.reraise import Reraise
try:
raise RuntimeError(first_caption)
except RuntimeError as e:
try:
Reraise(e, second_caption)
except RuntimeError as e2:
Reraise(e2, third_caption)
with pytest.raises(RuntimeError) as ex_info:
FunctionWithReraise()
exception_message = ExceptionToUnicode(ex_info.value)
for caption in [first_caption, second_caption, third_caption]:
assert caption in exception_message
def testExceptionToUnicodeOSError(
exception_message, lpe_exception_message, fse_exception_message
):
assert (
ExceptionToUnicode(OSError(2, exception_message))
== "[Errno 2] " + exception_message
)
assert (
ExceptionToUnicode(OSError(2, fse_exception_message))
== "[Errno 2] " + exception_message
)
assert (
ExceptionToUnicode(OSError(2, lpe_exception_message))
== "[Errno 2] " + exception_message
)
def testReraiseAddsMessagesCorrectly(exception_configuration):
with pytest.raises(exception_configuration.exception_type) as e:
exception_configuration.RaiseExceptionUsingReraise()
assert isinstance(e.value, exception_configuration.exception_type)
assert (
ExceptionToUnicode(e.value)
== exception_configuration.GetExpectedExceptionMessage()
)
traceback_message = traceback.format_exception_only(type(e.value), e.value)
traceback_message = b"".join(traceback_message)
if traceback_message != exception_configuration.GetExpectedTracebackMessage(
e.value, True
):
assert traceback_message == exception_configuration.GetExpectedTracebackMessage(
e.value, False
)
def testExceptionToUnicodeWithRaiseContext():
"""Ensure ExceptionToUnicode builds a message with all context/causes of an exception"""
with pytest.raises(RuntimeError) as exc_info:
def Original():
raise RuntimeError("Original Error")
try:
Original()
except RuntimeError:
raise RuntimeError("While handling error")
assert ExceptionToUnicode(exc_info.value) == "While handling error\nOriginal Error"
try:
raise RuntimeError('original message')
except Exception, e:
Reraise(e, '[message]', separator=' ')
>>> RuntimeError:
>>> [message] original message
'''
from barril._foundation.exceptions import ExceptionToUnicode
# IMPORTANT: Do NOT use try/except mechanisms in this method or the sys.exc_info()[-1] will be invalid
if hasattr(exception, 'reraised_message'):
current_message = exception.reraised_message
else:
current_message = ExceptionToUnicode(exception)
# Build the new message
if not current_message.startswith(separator):
current_message = separator + current_message
message = '\n' + message + current_message
if exception.__class__ in _SPECIAL_EXCEPTION_MAP:
# Handling for special case, some exceptions have different behaviors.
exception = _SPECIAL_EXCEPTION_MAP[exception.__class__](*exception.args)
elif exception.__class__ not in _SPECIAL_EXCEPTION_MAP.values():
# In Python 2.5 overriding the exception "__str__" has no effect in "unicode()". Instead, we
# must change the "args" attribute which is used to build the string representation.
# Even though the documentation says "args" will be deprecated, it uses its first argument
# in unicode() implementation and not "message".
exception.args = (message,)