From 40f653d0a6fec03419a6b6b3bd5b3032f7d915d6 Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 24 Mar 2021 13:11:28 +0100 Subject: [PATCH] allow pre_save_hook to cancel save with HTTPError currently, all errors in pre_save_hook are ignored, making it impossible for pre_save_hook to be used for validation / quota enforcement / etc. By allowing HTTPErrors to raise, save will be cancelled and the error message will be shown to the user --- jupyter_server/services/contents/manager.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jupyter_server/services/contents/manager.py b/jupyter_server/services/contents/manager.py index c91c4493cf..8d3d25ccb2 100644 --- a/jupyter_server/services/contents/manager.py +++ b/jupyter_server/services/contents/manager.py @@ -118,7 +118,13 @@ def run_pre_save_hook(self, model, path, **kwargs): try: self.log.debug("Running pre-save hook on %s", path) self.pre_save_hook(model=model, path=path, contents_manager=self, **kwargs) + except HTTPError: + # allow custom HTTPErrors to raise, + # rejecting the save with a message. + raise except Exception: + # unhandled errors don't prevent saving, + # which could cause frustrating data loss self.log.error("Pre-save hook failed on %s", path, exc_info=True) checkpoints_class = Type(Checkpoints, config=True)