Skip to main content

Error Collector

Test CodeQL Code Coverage Maintainability Dependabot PyPI - Python Version X URL

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)

Metadata

Release files for errorcollector 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for errorcollector 1.1.0
File Size Uploaded
errorcollector-1.1.0.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for errorcollector 1.1.0
File Interpreter ABI Platform
errorcollector-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / errorcollector-1.1.0.tar.gz

Download URL errorcollector-1.1.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
df94a4bf9bd57a701695bcf397bbdf9f067e03ff1e41bc941873691f509ed499
BLAKE2b-256 checksum
How to use checksums
353ae32ff46c4bf5f43149dfee46d35445ef18c866f3c6c587a8f5197c2b4435
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / errorcollector-1.1.0-py3-none-any.whl

Download URL errorcollector-1.1.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e085137d2ec0c79949fc8186684ac4fd46ca2e90b272333e7c9643294ae79c5
BLAKE2b-256 checksum
How to use checksums
a70b73a2d17512f8af3435e3a58210314889433e0aa77e0ba817b890a7152288
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page