Files
chia-blockchain/chia/util/log_exceptions.py
Matt HauffandGitHub 40db4635a8 [LABS-245] Enable PEP604 Ruff rules (#20269)
* Enable PEP604 Ruff rules

* Fix harcoded signature in test

* Hack CLVMStreamable test with note to fast follow
2025-11-18 12:34:00 -08:00

32 lines
751 B
Python

# Package: utils
from __future__ import annotations
import logging
import traceback
from collections.abc import Iterator
from contextlib import contextmanager
@contextmanager
def log_exceptions(
log: logging.Logger,
*,
consume: bool = False,
message: str = "Caught exception",
level: int = logging.ERROR,
show_traceback: bool = True,
exceptions_to_process: type[BaseException] | tuple[type[BaseException], ...] = Exception,
) -> Iterator[None]:
try:
yield
except exceptions_to_process as e:
message = f"{message}: {type(e).__name__}: {e}"
if show_traceback:
message += f"\n{traceback.format_exc()}"
log.log(level, message)
if not consume:
raise