pytoolbox.exceptions module

exception pytoolbox.exceptions.UndefinedPathError[source]

Bases: Exception

exception pytoolbox.exceptions.ForbiddenError[source]

Bases: Exception

A forbidden error.

exception pytoolbox.exceptions.InvalidIPSocketError(message=None, **kwargs)[source]

Bases: MessageMixin, Exception

message = '{socket} is not a valid IP socket.'
exception pytoolbox.exceptions.BadHTTPResponseCodeError(message=None, **kwargs)[source]

Bases: MessageMixin, Exception

message = 'Download request {url} code {r_code} expected {code}.'
exception pytoolbox.exceptions.InvalidBrandError(message=None, **kwargs)[source]

Bases: MessageMixin, Exception

message = 'Brand {brand} not in {brands}.'
exception pytoolbox.exceptions.CorruptedFileError(message=None, **kwargs)[source]

Bases: MessageMixin, Exception

message = 'File {path} is corrupted checksum {file_hash} expected {expected_hash}.'
exception pytoolbox.exceptions.MultipleSignalHandlersError(message=None, **kwargs)[source]

Bases: MessageMixin, Exception

message = 'Signal {signum} already handled by {handlers}.'
pytoolbox.exceptions.assert_raises_item(exception_cls, something, index, value=None, delete=False)[source]

Example usage

>>> x = {0: 3.14, 1: 2.54}

Assert that __getitem__ will fail:

>>> assert_raises_item(KeyError, x, 2)
>>> assert_raises_item(ValueError, x, 3)
Traceback (most recent call last):
    ...
ValueError: Exception KeyError is not an instance of ValueError.
>>> assert_raises_item(Exception, x, 0)
Traceback (most recent call last):
    ...
AssertionError: Exception Exception not raised.

Assert that __setitem__ will fail:

>>> assert_raises_item(TypeError, x, [10], value=3.1415)
>>> assert_raises_item(TypeError, x, 0, value=3.1415)
Traceback (most recent call last):
    ...
AssertionError: Exception TypeError not raised.

Assert that __delitem__ will fail:

>>> assert_raises_item(KeyError, x, 2, delete=True)
>>> assert_raises_item(KeyError, x, 1, delete=True)
Traceback (most recent call last):
    ...
AssertionError: Exception KeyError not raised.
>>> x == {0: 3.1415}
True
exception pytoolbox.exceptions.MessageMixin(message=None, **kwargs)[source]

Bases: Exception

__init__(message=None, **kwargs)[source]
message = None
pytoolbox.exceptions.get_exception_with_traceback(exception)[source]

Return a string with the exception traceback.

Example usage

If the exception was not raised then there are no traceback:

>>> get_exception_with_traceback(ValueError('yé'))
'ValueError: yé\n'

If the exception was raised then there is a traceback:

>>> try:
...     raise RuntimeError('yé')
... except Exception as ex:
...     trace = get_exception_with_traceback(ex)
>>> 'Traceback' in trace
True
>>> "raise RuntimeError('yé')" in trace
True