diff --git a/jupyter_cache/cache/db.py b/jupyter_cache/cache/db.py index 781a917..f5a20c2 100644 --- a/jupyter_cache/cache/db.py +++ b/jupyter_cache/cache/db.py @@ -65,7 +65,7 @@ def get_value(key: str, db: Engine, default=None): Setting.set_value(key, default, db) result = [default] else: - raise KeyError(key) + raise KeyError("Setting not found in DB: {}".format(key)) value = result[0] return value @@ -121,7 +121,9 @@ def record_from_hashkey(hashkey: str, db: Engine) -> "NbCacheRecord": session.query(NbCacheRecord).filter_by(hashkey=hashkey).one_or_none() ) if result is None: - raise KeyError(hashkey) + raise KeyError( + "Cache record not found for NB with hashkey: {}".format(hashkey) + ) session.expunge(result) return result @@ -130,7 +132,7 @@ def record_from_pk(pk: int, db: Engine) -> "NbCacheRecord": with session_context(db) as session: # type: Session result = session.query(NbCacheRecord).filter_by(pk=pk).one_or_none() if result is None: - raise KeyError(pk) + raise KeyError("Cache record not found for NB with PK: {}".format(pk)) session.expunge(result) return result @@ -139,7 +141,7 @@ def touch(pk, db: Engine): with session_context(db) as session: # type: Session record = session.query(NbCacheRecord).filter_by(pk=pk).one_or_none() if record is None: - raise KeyError(pk) + raise KeyError("Cache record not found for NB with PK: {}".format(pk)) record.accessed = datetime.utcnow() session.commit() @@ -150,7 +152,9 @@ def touch_hashkey(hashkey, db: Engine): session.query(NbCacheRecord).filter_by(hashkey=hashkey).one_or_none() ) if record is None: - raise KeyError(hashkey) + raise KeyError( + "Cache record not found for NB with hashkey: {}".format(hashkey) + ) record.accessed = datetime.utcnow() session.commit() @@ -260,7 +264,7 @@ def record_from_pk(pk: int, db: Engine) -> "NbStageRecord": with session_context(db) as session: # type: Session result = session.query(NbStageRecord).filter_by(pk=pk).one_or_none() if result is None: - raise KeyError(pk) + raise KeyError("Staging record not found for NB with PK: {}".format(pk)) session.expunge(result) return result @@ -269,7 +273,9 @@ def record_from_uri(uri: str, db: Engine) -> "NbStageRecord": with session_context(db) as session: # type: Session result = session.query(NbStageRecord).filter_by(uri=uri).one_or_none() if result is None: - raise KeyError(uri) + raise KeyError( + "Staging record not found for NB with URI: {}".format(uri) + ) session.expunge(result) return result @@ -292,7 +298,9 @@ def set_traceback(uri: str, traceback: Optional[str], db: Engine): with session_context(db) as session: # type: Session result = session.query(NbStageRecord).filter_by(uri=uri).one_or_none() if result is None: - raise KeyError(uri) + raise KeyError( + "Staging record not found for NB with URI: {}".format(uri) + ) result.traceback = traceback try: session.commit() diff --git a/jupyter_cache/cache/main.py b/jupyter_cache/cache/main.py index 00b059b..8bbba9b 100644 --- a/jupyter_cache/cache/main.py +++ b/jupyter_cache/cache/main.py @@ -279,7 +279,9 @@ def get_cache_bundle(self, pk: int) -> NbBundleOut: path = self._get_notebook_path_cache(record.hashkey) artifact_folder = self._get_artifact_path_cache(record.hashkey) if not path.exists(): - raise KeyError(pk) + raise KeyError( + "Notebook file does not exist for cache record PK: {}".format(pk) + ) return NbBundleOut( nbf.reads(path.read_text(), NB_VERSION), @@ -307,7 +309,9 @@ def remove_cache(self, pk: int): record = NbCacheRecord.record_from_pk(pk, self.db) path = self._get_notebook_path_cache(record.hashkey) if not path.exists(): - raise KeyError(pk) + raise KeyError( + "Notebook file does not exist for cache record PK: {}".format(pk) + ) shutil.rmtree(path.parent) NbCacheRecord.remove_records([pk], self.db)