tidyexc

raw JSON →
0.10.0 verified Sat May 09 auth: no python

tidyexc provides an exception class inspired by the tidyverse style guide for Python. It supports adding structured information to exceptions and formatting tracebacks in a clean, readable way. Current version is 0.10.0, released periodically.

pip install tidyexc
error NameError: name 'TidyError' is not defined
cause Mis-import: tidyexc exports Error, not TidyError.
fix
Replace 'from tidyexc import TidyError' with 'from tidyexc import Error'.
error TypeError: exceptions must derive from BaseException
cause Raising a tidyexc Error directly without wrapping it in a regular exception.
fix
Raise an instance of a builtin exception (e.g., RuntimeError) and attach tidyexc.Error via add_info, or use only_raise context manager.
error AttributeError: module 'tidyexc' has no attribute 'format_exc'
cause Using internal name; public function is format_exception_chain.
fix
Replace tidyexc.format_exc with tidyexc.format_exception_chain.
breaking In version 0.8.0, the internal use of format_exc() was removed. Code relying on that internal function must switch to format_exception_chain.
fix Use formatexceptionchain from tidyexc instead of any internal format_exc.
gotcha The Error class is not a subclass of BaseException, but a plain object that wraps exceptions. Catching Exception does not catch tidyexc errors.
fix Catch tidyexc.Error specifically, or use except Exception as e: only if the error is raised via raise Error('msg').
deprecated In version 0.7.0, add_info started supporting recursive calls. Older patterns that manually handle nested exceptions may break.
fix Upgrade to >=0.7.0 and use add_info recursively.
gotcha The put_info() function was added in version 0.8.0. It may conflict with add_info if used incorrectly.
fix Use put_info to attach information without modifying the exception chain.
breaking In version 0.5.1, the dotmap dependency was replaced with a non-recursive alternative. Code relying on dotmap's recursive behavior for nested attribute access may break.
fix Refactor code to not depend on recursive attribute access on info dicts.

Create a custom exception with tidyexc and attach additional context.

from tidyexc import Error, add_info

try:
    raise Error('Something went wrong')
except Error as e:
    add_info(e, user_id=42)
    raise