Utility wrapper to handle exceptions.
Project description
trier
Utility wrapper class to handle exceptions.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trier-1.0.6.tar.gz.
File metadata
- Download URL: trier-1.0.6.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
706ee7d996eba8e6f376f1a46d1ae419cc0580470279e6ca4a532ec322a500b5
|
|
| MD5 |
04c0c476cdea067ece0989402d80403c
|
|
| BLAKE2b-256 |
a97e0e0cc42c8096fde5238c4941ca2b3993622e4f604cff32d7f3add93605fa
|
File details
Details for the file trier-1.0.6-py3-none-any.whl.
File metadata
- Download URL: trier-1.0.6-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a3b0e3ea2b398a409923a5af7923b5c16b83c292f483f016f8ebe8ad6c40b6f
|
|
| MD5 |
1d81d881053759941129da6344e422ec
|
|
| BLAKE2b-256 |
66cd6b9c61a32fd5ff516a6b8781681fc7170b7c1ecd6dbdb116ae152baeb8d2
|