Skip to main content

Flexible failures collector with different collect strategies.

Project description

flexfail

Flexible failures collector with different collecting strategies.

flexfail provides a consistent and reusable way to collect, and handle failures. Useful in data processing, form validation, and other contexts where soft-failing logic is needed.


Justification

Why? Imagine you're processing a batch of data sent from a user and want to return a meaningful error description if something goes wrong. Suppose the data contains 3 different errors in separate places. In a naive implementation, you might return only one error per request. This would force the user to resubmit the request multiple times to fix everything.

This library aims to make error collection simpler, clearer, and more flexible.

It allows you to collect all errors in the data at once, if needed - or just return the first encountered error. You may even choose to skip invalid values silently. This behavior is controlled by predefined error collection strategies (see examples below).

Moreover, in our example, user may choose what strategy is more suitable for them.


Installation

pip install flexfail

Examples

Prerequisites before strategies overview

Below is a simple example of how flexfail can be used to wrap a function and handle failures using different strategies:

from flexfail import ErrorCollector, ErrorCollectorStrategy
from flexfail.exceptions import FlexFailException, FailFastException


# Let's assume, negative values are impossible to process, as the values are checkouts, for instance.
checkouts = [10, 20, -30, -40, 50, 'spam']


def process_check(value: float):
    if not isinstance(value, (float, int)):
        raise FlexFailException(data={'description': 'Checkout value is not a number!', 'value': value})
    if value < 0:
        raise FlexFailException(data={'description': 'Checkout value was below zero!', 'value': value})
    print(f'Check with amount {value}$ was successfully processed!')


def process_all_with_strategy(strategy: ErrorCollectorStrategy):
    error_collector = ErrorCollector(process_check, strategy)
    try:
        for _ in checkouts:
            error_collector.call(_)
    except FailFastException:
        pass
    print(f'Collected errors:')
    for error in error_collector.errors:
        print(error.data)

Please, note, only FlexFailException is safe to raise. If you need any other exception to be collected, just wrap it, using the data. You'll be able to access it later. Example:

raise FlexFailException(data={'my_wrapped_exception': RuntimeError('Some custom error!')})

Strategy skip

Force bypass all the errors and not even collect them.

process_all_with_strategy(ErrorCollectorStrategy.skip)

Results into:

Check with amount 10$ was successfully processed!
Check with amount 20$ was successfully processed!
Check with amount 50$ was successfully processed!
Collected errors:

Strategy fail_fast

Raise on first error occurs and collect only it.

process_all_with_strategy(ErrorCollectorStrategy.fail_fast)

Results into:

Check with amount 10$ was successfully processed!
Check with amount 20$ was successfully processed!
Collected errors:
{'description': 'Checkout value was below zero!', 'value': -30}

Strategy try_all

Collect all the errors.

process_all_with_strategy(ErrorCollectorStrategy.try_all)

Results into:

Collected errors:
{'description': 'Checkout value was below zero!', 'value': -30}
{'description': 'Checkout value was below zero!', 'value': -40}
{'description': 'Checkout value is not a number!', 'value': 'spam'}

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

flexfail-1.0.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

flexfail-1.0.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file flexfail-1.0.0.tar.gz.

File metadata

  • Download URL: flexfail-1.0.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for flexfail-1.0.0.tar.gz
Algorithm Hash digest
SHA256 180283d3d38317d4d203342346cf452f6ef0745a17db11c2ecf4efddfffaf92a
MD5 a2511ee4339e6772c7706a01d06653c8
BLAKE2b-256 781ab2d268b1ca9f1fb4c7483d5c35413eb9290b6ee6c616379cbb3caea840b4

See more details on using hashes here.

File details

Details for the file flexfail-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: flexfail-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for flexfail-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50d49f4d10e81aec1c8c032654c5c0e9ab671365049452599893ef29226f234f
MD5 c68fba67becec9497597d7e524111bc1
BLAKE2b-256 82c10bd075a1a1f193b908e58d40cd34eb52fa8141881d11bc91ce443685c74f

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