Skip to content

Commit

Permalink
Improve KeyError messages
Browse files Browse the repository at this point in the history
fixes #26
  • Loading branch information
chrisjsewell committed Mar 12, 2020
1 parent 6b2756d commit 3553e0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 16 additions & 8 deletions jupyter_cache/cache/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions jupyter_cache/cache/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 3553e0e

Please sign in to comment.