utils.error module

Handles errors based on something beyond the type. You can match error messages with regular expressions. You can also extend the matching behavior however you like. By default, strings are treated as regex and matched against the message of the error. Functions are passed the error and if the function returns ‘truthy’, then the error is caught.


import utils.error as error
with error.expected('foo'):
    x = 1
    raise Exception('oh noes foo happened!')  # this will be caught because regex matches

with error.expected('foo'):
    raise Exception('oh noes bar happened!')  # this will bubble up because it doesn't match

with error.expected('foo'):
    pass  # an error will be thrown because we expected an error but there wasn't one.
exception utils.error.UnexpectedSuccessException[source]

Bases: exceptions.Exception

An error that is thrown when something we expected to fail didn’t fail.

utils.error.expected(*args, **kwds)[source]

Inverts error handling. If the enclosed block doesn’t raise an error, it will raise one. If it raises a matching error, it will return normally. If it raises a non-matching error, that error will be allowed to propagate up the stack.

utils.error.handler(*args, **kwds)[source]

Handles errors based on more than just their type. Any matching error will be caught, the rest will be allowed to propagate up the stack.

utils.error.regex(expr, e)[source]

Search the message of the exception using the regex expr