A package allowing to define own error messages for Cerberus
Project description
Cerberror
Cerberror was designed to customize and handle errors generated by the validator of Cerberus.
How it works?
Let's start from checking errors generated by Cerberus:
>>> v = Validator({'params': {'type': 'dict', 'schema': {'var1': {'type': 'integer'}, 'var2': {'allowed': [7, 8, 9]}}}})
>>> v.validate({'params': {'var1': 'Hello World!', 'var2': 3.14}})
>>> v.errors
{'params': [{'var1': ['must be of integer type'], 'var2': ['unallowed value 3.14']}]}
At least two problems occur when we want to inform a user what went wrong:
- predetermined error messages,
- unclear path to wrong variable.
Let's define new messages in msgs.txt
file:
('params', 'var1') 36 "{{value}} is not an integer!"
('params', 'var2') 68 "{{value}} not found in {{constraint}}..."
where the columns contain:
- A path to the element defined by a tuple. Note that one-element tuple must be defined as
('some_param',)
, not'some_param',
. - An error code from the API documentation. A particular path can have many rules which implies many records in
msgs.txt
for that path. - A user-defined message. Optionally we can refer to attributes of a particular error using
{{attr}}
expressions. Note that a message must be defined between two"
, not'
.
To get new messages we use Translator
object:
>>> from cerberror import Translator
>>> tr = Translator(v, 'msgs.txt')
>>> tr.translate()
{'params -> var2': ['3.14 not found in [7, 8, 9]...'], 'params -> var1': ['Hello World! is not an integer!']}
Installation
To install the package please type from the command line:
$ pip3 install cerberror
Features
Cerberror has a few features which facilitate the usage of it.
Updates
Just like the validator of Cerberus, Translator
object can be updated at any time:
>>> tr = Translator(validator, 'messages.txt')
>>> tr.validator = another_validator
>>> tr.path_to_file = 'another_messages.txt'
This feature is particularly useful when we want to update files with messages on the fly. It could be used when we need to change language. Just define a proper file:
('params', 'var1') 36 "{{value}} ist keine ganze Zahl!"
('params', 'var2') 68 "{{value}} nicht unter {{constraint}} gefunden..."
Comments
Comments can be added to files with messages. These files are parsing with the usage of the regular expressions. Valid records are those defined in How it works? section. This means that everything else is treated as a comment. However, I recommend to use #
to mark where they start.
# Comment.
('params', 'var1') 36 "{{value}} is not an integer!" # Inline comment.
('params', 'var2') 68 "{{value}} not found in {{constraint}}..."
Paths
The returned paths are joined with usage of the default value ' -> '
. This behaviour can be changed easily:
>>> tr = Translator(v, 'msgs.txt')
>>> tr.translate('_')
{'params_var2': ['3.14 not found in [7, 8, 9]...'], 'params_var1': ['Hello World! is not an integer!']}
Returns
If the translation will finish successfully, the returned value will be a dictionary composed of path:message(s)
pairs. Otherwise, Translator
will return untouched errors generated by Cerberus. We can check the status of the translation using any_error
property:
>>> tr.any_error
False
>>> tr.errors
{'params -> var2': ['3.14 not found in [7, 8, 9]...'], 'params -> var1': ['Hello World! is not an integer!']}
...
>>> tr.any_error
True
>>> tr.errors
{'params': [{'var1': ['must be of integer type'], 'var2': ['unallowed value 3.14']}]} # v.errors
Internal errors
Internal errors can be inspected to find out what went wrong. The assumption was that Cerberror must not interrupt the translation process. If internal errors occur, any_error
property will return True
. Internal errors are always store in error_list
property. For example:
# foo and bar attributes don't exist
('params', 'var1') 36 "{{foo}} is not an integer!"
('params', 'var2') 68 "{{value}} not found in {{bar}}..."
>>> tr = Translator(v, 'msgs.txt')
>>> tr.translate()
{'params': [{'var1': ['must be of integer type'], 'var2': ['unallowed value 3.14']}]}
>>> tr.any_error
True
>>> tr.error_list
["Invalid expression '{{bar}}' in file 'msgs.txt'", "Invalid expression '{{foo}}' in file 'msgs.txt'"]
Contribution
New feature, bugs? Issues and pull requests are welcome.
License
Cerberror is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cerberror-0.1.0.tar.gz
.
File metadata
- Download URL: cerberror-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.20.1 setuptools/50.3.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccc01ce1026225e843fadcf65eb2dd6478d2e7a622f972c119ee6cb3e21c4895 |
|
MD5 | 7cc36187ed6ffd0d75314a49d7d86c16 |
|
BLAKE2b-256 | 16c605315580daef5abea7f2773a906f27c4c20f8a6f4f6c3eed6ce018852d4f |
File details
Details for the file cerberror-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: cerberror-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.20.1 setuptools/50.3.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54c8b0863f44a02848bc4b39b0f05b1640586c650cf565a8dfdd4e439e5aa301 |
|
MD5 | 6e5fe71de91ece87b05f5b8936cc9d31 |
|
BLAKE2b-256 | 12f24c16412a12ad19bb9c89b3796de9024854b8e5b3df4a754fc4db4af9e43b |