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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: trier-1.0.5.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.5.tar.gz
Algorithm Hash digest
SHA256 49827e9e4421b8036e21d39570ef95aea79ac6e5bf563b96f30f348f8431ef47
MD5 1a76c61c8bf86e54cc6f529fc53a34ba
BLAKE2b-256 4255f1fdad6fb96c7830c2fb92650824be81d2546c7621ea75b8969d6b2ecb17

See more details on using hashes here.

File details

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

File metadata

  • Download URL: trier-1.0.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c3f45f4cebd4430abe17953476b073f0ce7f8d5a6be850f8d32e9b456bb8ea7c
MD5 c3008379d6f1478119f112d8d231ac6a
BLAKE2b-256 25d19ad3045d1aa4b42bd94c3cfec9c39992882e79f08b0639242fbf8db42370

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