epy.maybe_reraise

Contents

epy.maybe_reraise#

etils.epy.maybe_reraise(prefix: str | Callable[[], str] | None = None, suffix: str | Callable[[], str] | None = None) Iterator[None][source]#

Context manager which reraise exceptions with an additional message.

Benefit: Contrary to raise … from … and raise Exception().with_traceback(tb), this function will:

  • Keep the original exception type, attributes,…

  • Avoid multi-nested During handling of the above exception, another exception occurred. Only the single original stacktrace is displayed.

This result in cleaner and more compact error messages.

Usage:

with epy.maybe_reraise(prefix=f'Error for {x}:'):
  fn(x)
Parameters:
  • prefix – Prefix to add to the exception message. Can be a function for lazy-evaluation.

  • suffix – Suffix to add to the exception message. Can be a function for lazy-evaluation.

Yields:

None