i2.errors¶
Error objects
Functions
|
Pass |
Classes
|
A context manager that catches and (specifically) handles specific exceptions. |
Context manager meant to ignore import errors. |
Exceptions
Base class for errors about what the caller is allowed to do. |
|
Base class for errors about the data itself. |
|
A |
|
An |
|
Raised for invalid input. |
|
Raise inside a |
|
A |
|
|
To raise when writes are only allowed if the item doesn't already exist |
- exception i2.errors.AuthorizationError[source]¶
Bases:
ExceptionBase class for errors about what the caller is allowed to do.
- exception i2.errors.DataError[source]¶
Bases:
ExceptionBase class for errors about the data itself.
- exception i2.errors.DuplicateRecordError[source]¶
Bases:
DataErrorA
DataErrorfor a record that already exists.
- exception i2.errors.ForbiddenError[source]¶
Bases:
AuthorizationErrorAn
AuthorizationErrorfor an operation that is not allowed.
- class i2.errors.HandleExceptions(on_error=<factory>)[source]¶
Bases:
AbstractContextManagerA context manager that catches and (specifically) handles specific exceptions.
It takes one argument: A dict (or mapping) of exception type keys and callback values. If within a with block, the particular (listed) exception happens, the callback is called and it’s returned value is assigned to the
HandleExceptionsinstance’s.exit_valueattribute. That attribute will only exist if the with block existed with an exception caught byHandleExceptions.A callback is an argument-less function. If you need to specify arguments, you can envoke the command pattern, using
functools.partialto make a argument-less function. See in the example below how we askHandleExceptionsto print a specific string if aZeroDivisionErrorhappens:>>> from functools import partial >>> def print_and_return(msg): ... print(msg) ... return msg >>> with HandleExceptions({ ... ZeroDivisionError: partial(print_and_return, "You interrupted me"), ... KeyboardInterrupt: lambda: 'imagine this is code to notify someone' ... }) as he: ... print('This works') ... 0 / 0 ... This works You interrupted meYou can check if the context exited with a handled exception, and if so, what the callback returned value was.
>>> he.exited_with_handled_exception() True >>> he.exit_value 'You interrupted me'Also available, whether the exception was a handled one or not: The exception instance itself:
>>> he.exited_with_exception ZeroDivisionError('division by zero')If all you want to do though is print a string (and have the same string available in the
exit_valueattribute, we got you covered! Just specify a string and we’ll make that printer callaback for you!>>> from functools import partial >>> >>> with HandleExceptions({ZeroDivisionError: "You interrupted me again!"}): ... print('This also works') ... 0 / 0 This also works You interrupted me again!Note that specifying
partial(print, "some message")will work as a “printing callback”, but the string won’t be available in.exit_valuesinceprintreturns None.A few recipes now…
You can also use your own custom exception types to do things like interrupt a with block early given some condition(s).
>>> with HandleExceptions( ... {InterruptWithBlock: "The with block was interrupted early."} ... ): ... print('before condition') ... x = 5 % 2 ... if x: ... raise InterruptWithBlock() ... print('after condition') ... ... before condition The with block was interrupted early.Tip
If you need to do stuff with an exception, but reraise it, you can still do that in your callback. Just say
raiseat the end of the callback!>>> def print_and_raise(msg): ... print(msg) ... raise >>> >>> with HandleExceptions({ ... ZeroDivisionError: partial(print_and_raise, "That again!"), ... }): ... print('This also works') ... 0 / 0 This also works That again! Traceback (most recent call last): ... ZeroDivisionError: division by zero
- exception i2.errors.InterruptWithBlock[source]¶
Bases:
BaseExceptionRaise inside a
withblock to leave it early; pair withHandleExceptions.>>> with HandleExceptions({InterruptWithBlock: "stopped early"}) as h: ... raise InterruptWithBlock() ... print("never printed") stopped early >>> h.exit_value 'stopped early'
- class i2.errors.ModuleNotFoundIgnore[source]¶
Bases:
objectContext manager meant to ignore import errors. The use case in mind is when we want to condition some code on the existence of some package.
- exception i2.errors.NotFoundError[source]¶
Bases:
DataErrorA
DataErrorfor a record that does not exist.
- exception i2.errors.OverwritesNotAllowed(*args, forbidden_keys=None, **kwargs)[source]¶
Bases:
AuthorizationErrorTo raise when writes are only allowed if the item doesn’t already exist