Skip to main content

This project helps you to collect raised error during with statement.

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

errorcollector-1.0.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

errorcollector-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file errorcollector-1.0.1.tar.gz.

File metadata

  • Download URL: errorcollector-1.0.1.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for errorcollector-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1617b499fada97fa06a2f62f395119aa514325a0d2d105cf4f46a508c38148f1
MD5 c78ac9628ee76e6f413815d22a5da39f
BLAKE2b-256 ac6e4ac9213bb280bdadb479326daf4012899ab9dd2fcd9308ba3ab61ad64524

See more details on using hashes here.

File details

Details for the file errorcollector-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: errorcollector-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for errorcollector-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5bf65b22da2e1fdd576df4183a9455f9ac37ee5ef2962dec269a0ead5fea1b8f
MD5 e955ce235ee87d100de9cd622e07ac30
BLAKE2b-256 dc68c2fbb71304b3a11017b156d65615b9b6c6c1d0f715bd1d216326884dba53

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page