Skip to content

Commit

Permalink
Project: Don't funnel ::load() errors into RuntimeErrors
Browse files Browse the repository at this point in the history
Instead, let the exceptions propagate up to the caller.  For one thing,
this allows us to write more specific tests :^)
  • Loading branch information
keggsmurph21 committed Dec 20, 2019
1 parent fa457a1 commit 7450611
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
15 changes: 6 additions & 9 deletions ultratrace2/model/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ def save(self):

@classmethod
def load(cls, save_file: str) -> "Project":
try:
with open(save_file, "rb") as fp:
project = pickle.load(fp)
assert isinstance(project, Project)
return project
except Exception as e:
logger.error(e)
raise RuntimeError(str(e))
with open(save_file, "rb") as fp:
project = pickle.load(fp)
assert isinstance(project, Project)
return project

@classmethod
def get_by_path(cls, root_path: str) -> "Project":
Expand All @@ -51,7 +47,8 @@ def get_by_path(cls, root_path: str) -> "Project":
save_file = cls.get_save_file(root_path)
try:
return cls.load(save_file)
except RuntimeError:
except Exception as e:
logger.warning(e)
logger.info(
f"Unable to find existing project at {root_path}, creating new one..."
)
Expand Down
8 changes: 4 additions & 4 deletions ultratrace2/model/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@pytest.mark.parametrize(
"save_file,error",
[
("", RuntimeError),
("/path/to/nowhere", RuntimeError),
("/dev/null", RuntimeError), # pickle cannot open
("/etc/sudoers", RuntimeError), # not readable
("", FileNotFoundError),
("/path/to/nowhere", FileNotFoundError),
("/dev/null", EOFError), # pickle cannot open
("/etc/sudoers", PermissionError), # not readable
],
)
def test_load_project_invalid(save_file: str, error: Exception) -> None:
Expand Down

0 comments on commit 7450611

Please sign in to comment.