@contextlib.contextmanager def add_exc_note(note: str): try: yield except Exception as err: err.add_note(note) raise with add_exc_note(f"While attempting to frobnicate {item=}"): frobnicate_or_raise(item)