Skip to main content

Utility wrapper to handle exceptions.

Project description

trier

Utility wrapper class to handle exceptions.

PyPI version trier CI codecov PyPI - Python Version

Supported Python versions

Python >= 3.8

Installation

You can install trier from PyPI, with pip:

python -m pip install trier

Example usage

Instead of using try except block you could replace it with trier.

from trier import Try

err, val = Try(lambda: 10 / 0).catch(ZeroDivisionError)

if err:
    # handle error

# do stuff with `val`

# Another way to handle could be
if not err:
  # do stuff with `val`

Supports multiple exceptions.

from trier import Try

err, file = Try(open, file="doenot_exist.txt").catch(FileNotFoundError, OSError)

if err:
    # handle error

# do stuff with `file`

Supports async error handling as well.

import asyncio
from httpx import AsyncClient, HTTPStatusError, Response
from trier import Try

def raise_on_4xx_5xx(response):
    response.raise_for_status()

async def main():
    client = AsyncClient(event_hooks={"response": [raise_on_4xx_5xx]})

    # The endpoint responds with a 404 error
    err, response = await Try(client.get, "https://run.mocky.io/v3/201f1fe6-5a3b-49c1-9df7-312951618405").async_catch(HTTPStatusError)

    if err:
        # Handle error

    # do stuff with `response`

asyncio.run(main())

Changelog

Refer to the CHANGELOG.

License

MIT - See the LICENSE for more information.

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

trier-1.0.6.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

trier-1.0.6-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

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