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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: trier-1.0.1.tar.gz
  • Upload date:
  • Size: 3.5 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.1.tar.gz
Algorithm Hash digest
SHA256 3c9915ee40fb96fecacc23834116bba8efebb022ef0451c7025a6c50a76c6599
MD5 645c589ea16131058647a8cb2ab102c8
BLAKE2b-256 07405291cc83966770ba4bdfbd11f1d36519441b5bf7cbae7a8c4795c968604b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: trier-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd1310aa62f1379bbc62f35aad05b507116893dddab2d8dd1e29b117d979e6b9
MD5 4846f8fc2c1fe49eb559626b1c8cefc8
BLAKE2b-256 9e62665bd33e9b6755b55a0191d8358cc7adea703369b7b4c6986f7c79760ab9

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