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

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.md file.

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

Uploaded Source

Built Distribution

trier-1.0.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: trier-1.0.2.tar.gz
  • Upload date:
  • Size: 3.6 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.2.tar.gz
Algorithm Hash digest
SHA256 66032f7ce8fecad43eb9201617e45616105db7aaa944ff8efd352a691e244d18
MD5 683a6f1fbe7db408402548f0ce6d1903
BLAKE2b-256 bc70f94efa57dbd0f54a31b00ab254bbdae9effe3d6554cb0c210df2ade92037

See more details on using hashes here.

File details

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

File metadata

  • Download URL: trier-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 565ee340c8cc3e819cf49c35a5094e3592cf86f80b7b3f4fd6caa27d6c2f832f
MD5 5c38afa3bb7bf1bd180d5834e8972b88
BLAKE2b-256 34205a3d24d93206c9c8e30b378bf818602c3f318e1ea2a920a74cabe5766231

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