Skip to main content

Seviper - Error handling framework to catch 'em all

Unittests status badge Coverage status badge Linting status badge Black status badge

Features

This framework provides several error handlers to catch errors and call callback functions to handle these errors (or successes). It comes fully equipped with:

  • A decorator to handle errors in functions or coroutines
  • A decorator to retry a function or coroutine if it fails (can be useful for network requests)
  • A context manager to handle errors in a block of code

Additionally, if you use aiostream (e.g. using pip install seviper[aiostream]), you can use the following features:

  • The stream.map (or pipe.map, analogous to the aiostream functions) function to run the function, catch all exceptions and call the error handler if an exception occurs. Additionally, filters out all failed results.

Installation

pip install seviper

or optionally:

pip install seviper[aiostream]

Usage

Here is a more or less complex example as showcase of the features of this library:

import asyncio
import logging
import sys
import aiostream
import error_handler

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, force=True)
logger = logging.root
op = aiostream.stream.iterate(range(10))


def log_error(error: Exception, num: int):
    """Only log error and reraise it"""
    logger.error("double_only_odd_nums_except_5 failed for input %d. ", num)
    raise error


@error_handler.decorator(on_error=log_error)
async def double_only_odd_nums_except_5(num: int) -> int:
    if num % 2 == 0:
        raise ValueError(num)
    with error_handler.context_manager(on_success=lambda: logging.info("Success: %s", num)):
        if num == 5:
            raise RuntimeError("Another unexpected error. Number 5 will not be doubled.")
        num *= 2
    return num


def catch_value_errors(error: Exception, _: int):
    if not isinstance(error, ValueError):
        raise error


def log_success(result_num: int, provided_num: int):
    logger.info("Success: %d -> %d", provided_num, result_num)


op = op | error_handler.pipe.map(
    double_only_odd_nums_except_5,
    on_error=catch_value_errors,
    on_success=log_success,
    wrap_secured_function=True,
    suppress_recalling_on_error=False,
)

result = asyncio.run(aiostream.stream.list(op))

assert result == [2, 6, 5, 14, 18]

This outputs:

ERROR:root:double_only_odd_nums_except_5 failed for input 0.
INFO:root:Success: 2
INFO:root:Success: 1 -> 2
ERROR:root:double_only_odd_nums_except_5 failed for input 2.
INFO:root:Success: 6
INFO:root:Success: 3 -> 6
ERROR:root:double_only_odd_nums_except_5 failed for input 4.
INFO:root:Success: 5 -> 5
ERROR:root:double_only_odd_nums_except_5 failed for input 6.
INFO:root:Success: 14
INFO:root:Success: 7 -> 14
ERROR:root:double_only_odd_nums_except_5 failed for input 8.
INFO:root:Success: 18
INFO:root:Success: 9 -> 18

How to use this Repository on Your Machine

Please refer to the respective section in our Python template repository to learn how to use this repository on your machine.

Contribute

You are very welcome to contribute to this template repository by opening a pull request against the main branch.

Download files

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

Source Distribution

seviper-0.1.2.tar.gz (69.1 kB view details)

Uploaded Source

Built Distribution

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

seviper-0.1.2-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file seviper-0.1.2.tar.gz.

File metadata

  • Download URL: seviper-0.1.2.tar.gz
  • Upload date:
  • Size: 69.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for seviper-0.1.2.tar.gz
Algorithm Hash digest
SHA256 379089fd5f12973251c87be9ae4d013cc3159eed3c73b15159cbcb1731ce2f53
MD5 07ed94160fd4d395a456fb4b4a462980
BLAKE2b-256 fd6334266a7ec0a7e71f214acd20685cb1e30a23bd76dadfc3e9d6c6a5a4b735

See more details on using hashes here.

File details

Details for the file seviper-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: seviper-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for seviper-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 093db3838db034adeb8d285edbe2b48ff4a52925ae2e0bc5cdc17fdbd31236d5
MD5 26fa25fdba6a1c96b96c285013e857fd
BLAKE2b-256 a872b9cd43177d3e5fffac958ae565156d46f36e246f82af72ff4ea7bbd59097

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 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