coarnotify.exceptions¶
Module for custom exceptions
Exceptions¶
Base class for all exceptions in the coarnotifypy library |
|
Exception class for validation errors. |
Module Contents¶
- exception coarnotify.exceptions.NotifyException¶
Bases:
Exception
Base class for all exceptions in the coarnotifypy library
- exception coarnotify.exceptions.ValidationError(errors: dict = None)¶
Bases:
NotifyException
Exception class for validation errors.
- Parameters:
errors – a dictionary of errors to construct the exception around. See below for the details of its structure
This class is designed to be thrown and caught and to collect validation errors as it passed through the validation pipeline.
For example an object validator may do something like this:
def validate(): ve = ValidationError() ve.add_error(prop_name, validate.REQUIRED_MESSAGE.format(x=pn)) if ve.has_errors(): raise ve return True
If this is called by a subclass which is also validating, then this may be used like this:
def validate(): ve = ValidationError() try: super(ClassName, self).validate() except ValidationError as superve: ve = superve ve.add_error(prop_name, validate.REQUIRED_MESSAGE.format(x=pn)) if ve.has_errors(): raise ve return True
By the time the ValidationError is finally raised to the top, it will contain all the validation errors from the various levels of validation that have been performed.
The errors are stored as a multi-level dictionary with the keys at the top level being the fields in the data structure which have errors, and within the value for each key there are two possible keys:
errors: a list of error messages for this field
nested: a dictionary of further errors for nested fields
{ "key1": { "errors": ["error1", "error2"], "nested: { "key2": { errors: ["error3"] } } } }
- add_error(key: str, value: str)¶
Record an error on the supplied
key
with the messagevalue
- Parameters:
key – the key for which an error is to be recorded
value – the error message
- Returns:
- add_nested_errors(key: str, subve: ValidationError)¶
Take an existing ValidationError and add it as a nested set of errors under the supplied key
- Parameters:
key – the key under which all the nested validation errors should go
subve – the existing ValidationError object
- Returns:
- has_errors() bool ¶
Are there any errors registered
- property errors: dict¶
The dictionary of errors