from functools import wraps def handle_raise_on_commit(f): @wraps(f) def inner(self, *args, **kwargs): try: f(self, *args, **kwargs) return True except: # noqa: E722 # Unexpected error occurred, rollback all changes and log message LOGGER.exception("Unexpected error") if self.raise_on_commit: raise return False return inner