errortools - a toolset for working with Python exceptions and warnings and logging.
Project description
errortools
A lightweight Python exception handling utility library.
Installation
Use pip...
pip install errortools
...or uv
uv add errortools
Features
- Suppress:
ignore(),super_fast_ignore(),@suppress()— silence exceptions gracefully - Retry & Timeout:
@retry(),@timeout()— auto retry with delay, async timeout - Raise & Convert:
raises(),reraise(),@convert()— batch raise, type conversion - Catch & Collect:
super_fast_catch(),ExceptionCollector— inspect or batch exceptions - Caching:
@error_cache— LRU exception cache, likefunctools.lru_cache - Custom Exceptions:
PureBaseException,ContextException,BaseErrorCodes - Logging: structured logger with sinks, context binding, and exception capture
Quick Start
from errortools import ignore, retry, reraise, error_cache, suppress, convert
from errortools.future import super_fast_ignore, super_fast_catch, ExceptionCollector
# ── Suppress ─────────────────────────────────────────────────
with ignore(KeyError) as err: # full metadata
_ = {}["missing"]
# err.be_ignore=True, err.name='KeyError', err.traceback=...
with super_fast_ignore(ValueError): # zero-overhead
int("bad")
@suppress(ZeroDivisionError, default=0) # decorator form
def divide(a, b): return a / b
# ── Retry ────────────────────────────────────────────────────
@retry(times=3, on=ConnectionError, delay=1.0)
def connect(host: str): ...
# ── Convert ──────────────────────────────────────────────────
@convert(KeyError, ValueError) # decorator
def lookup(d, key): return d[key]
with reraise(KeyError, ValueError): # context manager
raise KeyError("x") # → ValueError
# ── Catch & Collect ──────────────────────────────────────────
with super_fast_catch(ValueError) as ctx:
raise ValueError("oops")
# ctx.exception → ValueError('oops')
collector = ExceptionCollector()
collector.catch(int, "bad1")
collector.catch(int, "bad2")
collector.raise_all() # → ExceptionGroup
# ── Cache ────────────────────────────────────────────────────
@error_cache(maxsize=64)
def load(uid: int):
if uid < 0: raise ValueError(f"invalid: {uid}")
return {"id": uid}
Custom Exceptions
from errortools import PureBaseException, ContextException, BaseErrorCodes
class AppError(PureBaseException):
code = 9000
default_detail = "Application error."
err = ContextException("failed").with_context(service="db")
raise BaseErrorCodes.not_found("user #42") # NotFoundError [3001]
Logging
from errortools.logging import logger
logger.info("Server started on port {}", 8080)
logger.add("app.log", rotation=10_485_760, retention=5)
with logger.catch():
int("not a number") # logged + suppressed
Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
errortools-3.2.0.tar.gz
(64.3 kB
view details)
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 errortools-3.2.0.tar.gz.
File metadata
- Download URL: errortools-3.2.0.tar.gz
- Upload date:
- Size: 64.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a152b750dabb1d156baa8f16f689ace9ed175294fd9298795f547c3a6640c17
|
|
| MD5 |
180d7f7870a1582a7237a3653cfec58e
|
|
| BLAKE2b-256 |
dabbb06359c40fdf838ce6302d262f37839737676996c03ad73130d179787a6b
|
File details
Details for the file errortools-3.2.0-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: errortools-3.2.0-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 85.7 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
946f4a157d4cc09974b34769b25a6c07059890cbc7c54897d70c7636194b58cb
|
|
| MD5 |
d34365a7e41dc6031bd58c3245a73d2d
|
|
| BLAKE2b-256 |
3816d13aeca608dbd05427614ed97507c171697bc7caa6b33e1d0547194a1087
|