Skip to content

Commit

Permalink
refactor: Save exception messages as variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgate committed Apr 24, 2024
1 parent cef4d26 commit cb3c78e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion shell_logger/html_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def _append_html(
elif isinstance(arg, Iterable):
_append_html(f, *arg)
else:
raise RuntimeError(f"Unsupported type: {type(arg)}")
message = f"Unsupported type: {type(arg)}"
raise RuntimeError(message)

with open(output, "a") as output_file:
_append_html(output_file, *args)
Expand Down
5 changes: 3 additions & 2 deletions shell_logger/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ def run(self, command: str, **kwargs) -> SimpleNamespace:
# KeyboardInterrupt.
except KeyboardInterrupt:
os.close(self.aux_stdin_wfd)
raise RuntimeError(
message = (
f"There was a problem running the command `{command}`. "
"This is a fatal error and we cannot continue. Ensure that "
"the syntax of the command is correct."
) from None
)
raise RuntimeError(message) from None
finish = round(time() * milliseconds_per_second)

# Pull the return code and return the results. Note that if the
Expand Down
8 changes: 4 additions & 4 deletions shell_logger/shell_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ def append(path: Path) -> ShellLogger:
try:
path = next(path.glob("*.html"))
except StopIteration as error:
raise RuntimeError(
f"{path} does not have an html file."
) from error
message = f"{path} does not have an html file."
raise RuntimeError(message) from error
if path.is_symlink():
path = path.resolve(strict=True)
if path.is_file() and path.name[-5:] == ".html":
Expand Down Expand Up @@ -290,10 +289,11 @@ def change_log_dir(self, new_log_dir: Path) -> None:
:class:`ShellLogger`.
"""
if not self.is_parent():
raise RuntimeError(
message = (
"You should not change the log directory of a child "
"`ShellLogger`; only that of the parent."
)
raise RuntimeError(message)

# This only gets executed once by the top-level parent
# `ShellLogger` object.
Expand Down
6 changes: 4 additions & 2 deletions shell_logger/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ def trace_collector(**kwargs) -> Trace:
collector = collectors[0]
return collector(**kwargs)
elif len(collectors) == 0:
raise RuntimeError(f"Unsupported trace type: {trace_name}")
message = f"Unsupported trace type: {trace_name}"
raise RuntimeError(message)
else:
raise RuntimeError(f"Multiple trace types match '{trace_name}'.")
message = f"Multiple trace types match '{trace_name}'."
raise RuntimeError(message)


class Trace:
Expand Down

0 comments on commit cb3c78e

Please sign in to comment.