Skip to main content

Prettier error handling for Python

Project description

Tri/bi

This package provides wrappers around functions that nicely handle errors. Reducing code clutter and allowing better responses to uses. This package is inspired by tri-fp

Tri

Tri still lets native exceptions (usually more severe errors) throw, but catches any other errors

>>> from tri import tri, bi
>>> def my_function():
...    return 1 / 0 # Zero division error!

>>> # This function raises a "native" exception
>>> tri(my_function)()
Traceback (most recent call last):
   ...
ZeroDivisionError: division by zero

>>> def my_other_function():
...    raise Exception

>>> # This only raises a standard exception, so doesn't fail
>>> tri(my_other_function)()
(Exception(), None)

Bi

Bi, on the contrary, handles all errors blindly. This should only be used if you know what you're doing.

>>> bi(my_function)()
(ZeroDivisionError('division by zero'), None)

Async

Both tri and bi support async.

Tri

>>> # Assuming running in async
>>> from tri import atri, abi
>>> async def divide(a, b):
...     return a / b

>>> # Let's try to divide by zero
>>> await atri(divide(1, 0))
Traceback (most recent call last):
   ...
ZeroDivisionError: division by zero

>>> # And with abi?
>>> await abi(divide(1, 0))
(ZeroDivisionError('division by zero'), None)

>>> # If the function takes no params, we can just pass the function name
>>> async def func():
...     return "hello"

>>> await atri(func)
(None, "hello")

Real life example

Some examples include...

Safer JSON loading

from tri import bi

safer_loads = bi(json.loads)
error, result = safer_loads(data)

if error:
   # Error handling JSON
   print("Invalid JSON")

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

tribi-1.0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

tribi-1.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tribi-1.0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for tribi-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e070d0e852fe05d375490fd8edbc2d1c7bb65a9461539a2428da82b46170d837
MD5 4fd7ce80d81b17488d4ae43a4bccb746
BLAKE2b-256 39e4979f80af8b2d8a7ff2b48705934c1c93fe51e400a36bab8a5e752395af69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tribi-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for tribi-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c656d48537783d167aab4a1a955a8a23710906e0790b7827e8df49b0c86c6aa
MD5 5f83e0569f805f6f74fce98f07d632e7
BLAKE2b-256 fdedecb1aba47ad8252ae22d9fdb8f146815f1d30eeba903d0ff41ab29128deb

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