Skip to main content

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.

Metadata

Release files for trier 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trier 1.0.6
File Size Uploaded
trier-1.0.6.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trier 1.0.6
File Interpreter ABI Platform
trier-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / trier-1.0.6.tar.gz

Download URL trier-1.0.6.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
706ee7d996eba8e6f376f1a46d1ae419cc0580470279e6ca4a532ec322a500b5
BLAKE2b-256 checksum
How to use checksums
a97e0e0cc42c8096fde5238c4941ca2b3993622e4f604cff32d7f3add93605fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.9.4 Darwin/21.6.0

Release files / trier-1.0.6-py3-none-any.whl

Download URL trier-1.0.6-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8a3b0e3ea2b398a409923a5af7923b5c16b83c292f483f016f8ebe8ad6c40b6f
BLAKE2b-256 checksum
How to use checksums
66cd6b9c61a32fd5ff516a6b8781681fc7170b7c1ecd6dbdb116ae152baeb8d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.9.4 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page