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.4.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

trier-1.0.4-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file trier-1.0.4.tar.gz.

File metadata

  • Download URL: trier-1.0.4.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.9.4 Darwin/21.6.0

File hashes

Hashes for trier-1.0.4.tar.gz
Algorithm Hash digest
SHA256 254e348a959f988ddf8135275217313c84684e56ba1739991199ef5db620c342
MD5 5e6fbdd5b90c03d75cebeb6b6aff4a4e
BLAKE2b-256 9a573fac015667c5e64c99288dbf75de5da4ecfe9030d6348cfe6fc0a47ae5e3

See more details on using hashes here.

File details

Details for the file trier-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: trier-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.9.4 Darwin/21.6.0

File hashes

Hashes for trier-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 afc9c79842d7fa2b47014e5756407bf6e44ca99fd4297e163a90b41b176a943d
MD5 e3b309c13d1884d86649bf37d266779f
BLAKE2b-256 dab2e60f2369b5a88460a0e092cecf1dfdefca1a65cb8773010d266ab0e7f955

See more details on using hashes here.

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