This project helps you to collect raised error during with statement.
Project description
Error Collector
Collects raised error during with statement.
Features
In some cases we don't want to raise an error immediately. For example, case when return error HTTP response to client after validating whole HTTP POST data.
This package helps to collect error.
Installation
pip install errorcollector
Usage
MultipleErrorCollector
Let's say, there are data model which has single property. Before process this data model, we want to validate property.
Ex:
from yourproduct.exceptions import ConvertError
class PostDataModel:
def __init__(self, property_a_string: str, property_b_string: str):
self._property_a_string = property_a_string
self._property_b_string = property_b_string
self.list_error = None
def validate(self) -> bool:
self.stock_convert_error(
lambda: self.property_a_int,
f"Property string A couldn't be converted to integer. Property string = {self._property_a_string}"
)
self.stock_convert_error(
lambda: self.property_b_int,
f"Property string B couldn't be converted to integer. Property string = {self._property_b_string}"
)
return bool(self.list_error)
def stock_convert_error(self, method: Callable[[], Any], message: str) -> None:
with MultipleErrorCollector(ConvertError, message, self.list_error):
return method()
@property
def property_a_int() -> int:
"""May raise ValueError"""
return int(self._property_a_string)
@property
def property_b_int() -> int:
"""May raise ValueError"""
return int(self._property_b_string)
When we call method validate()
, even if ValueError
occurs,
the exception is not raised and execution does not stop.
When method()
in method stock_convert_error()
raises ValueError
,
ConvertError
which is set raised ValueError
into __cause__
is set
into property self.list_error
.
We can check details of error after validate.
SingleErrorCollector
This is single version of Error Collector. This may be useful in case when need to handle multiple cases and singular cases in an integrated method by polymorphism.
Ex:
from yourproduct.exceptions import ConvertError
class PostDataModel:
def __init__(self, property_string: str):
self._property_string = property_string
self.convert_error = None
def validate(self) -> bool:
self.stock_convert_error(
lambda: self.property_int,
f"Property string couldn't be converted to integer. Property string = {self._property_string}"
)
return self.convert_error is not None
def stock_convert_error(self, method: Callable[[], Any], message: str) -> None:
error_collector = SingleErrorCollector(ConvertError, message)
with error_collector:
method()
self.convert_error = error_collector.error
@property
def property_int() -> int:
"""May raise ValueError"""
return int(self._property_string)
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 errorcollector-1.0.0.tar.gz
.
File metadata
- Download URL: errorcollector-1.0.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34a1d70b0fe3025da1be450372369202b65fe3fc455dcd07b0cc826d5794a0fc |
|
MD5 | 6c8527a21fe4bc46c5a8b32d6eb2b927 |
|
BLAKE2b-256 | a79dbf010769982161146b4ce17b77b66f8cd80dd6daf07452df0cc4a4346ada |
File details
Details for the file errorcollector-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: errorcollector-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81471f593768ccc73b2e7f9ca22f2a327328c21596aba7ebdc8ab22874f2f597 |
|
MD5 | a5c8eb5c4edf79082ff78520735b5acf |
|
BLAKE2b-256 | 78807b5bf042cdec91033343cbe748967f67236554146ad9557f03c36156d7f0 |