Metadata-Version: 2.4
Name: errorcollector
Version: 1.0.1
Summary: This project helps you to collect raised error during with statement.
Author-email: Yukihiko Shinoda <yuk.hik.future@gmail.com>
Maintainer-email: Yukihiko Shinoda <yuk.hik.future@gmail.com>
License: MIT License
        
        Copyright (c) 2020 Yukihiko Shinoda
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: homepage, https://github.com/yukihiko-shinoda/error-collector
Project-URL: repository, https://github.com/yukihiko-shinoda/error-collector
Keywords: error,exception
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Utilities
Classifier: Typing :: Typed
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Error Collector

[![Test](https://github.com/yukihiko-shinoda/error-collector/workflows/Test/badge.svg)](https://github.com/yukihiko-shinoda/error-collector/actions?query=workflow%3ATest)
[![CodeQL](https://github.com/yukihiko-shinoda/error-collector/workflows/CodeQL/badge.svg)](https://github.com/yukihiko-shinoda/error-collector/actions?query=workflow%3ACodeQL)
[![Code Coverage](https://qlty.sh/gh/yukihiko-shinoda/projects/error-collector/coverage.svg)](https://qlty.sh/gh/yukihiko-shinoda/projects/error-collector)
[![Maintainability](https://qlty.sh/gh/yukihiko-shinoda/projects/error-collector/maintainability.svg)](https://qlty.sh/gh/yukihiko-shinoda/projects/error-collector)
[![Dependabot](https://flat.badgen.net/github/dependabot/yukihiko-shinoda/error-collector?icon=dependabot)](https://github.com/yukihiko-shinoda/error-collector/security/dependabot)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/errorcollector)](https://pypi.org/project/errorcollector/)
[![X URL](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fyukihiko-shinoda%2Ferror-collector)](https://x.com/intent/post?text=Error%20Collector&url=https%3A%2F%2Fpypi.org%2Fproject%2Ferrorcollector%2F&hashtags=python)

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

```console
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:

```python
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:

```python
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)
```
