_exceptions.py 380 B

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