_exceptions.py 452 B

12345678910111213141516171819
  1. import contextlib
  2. from typing import Tuple
  3. @contextlib.contextmanager
  4. def rewrite_exception(old_name: str, new_name: str):
  5. """
  6. Rewrite the message of an exception.
  7. """
  8. try:
  9. yield
  10. except Exception as err:
  11. msg = err.args[0]
  12. msg = msg.replace(old_name, new_name)
  13. args: Tuple[str, ...] = (msg,)
  14. if len(err.args) > 1:
  15. args = args + err.args[1:]
  16. err.args = args
  17. raise