generated from ACCESS-NRI/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new exception class to reveal more information to the user. Fixes
- Loading branch information
1 parent
eb901be
commit 7ced15e
Showing
4 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,36 @@ | ||
import subprocess as sp | ||
|
||
|
||
class NoClientException(Exception): | ||
def __init__(self): | ||
super().__init__( | ||
"Unable to detect scheduler type, cannot determine client type." | ||
) | ||
|
||
|
||
class ShellException(Exception): | ||
"""Exception class to improve readability of the subprocess.CalledProcessError""" | ||
|
||
def __init__(self, called_process_error: sp.CalledProcessError): | ||
"""Constructor | ||
Parameters | ||
---------- | ||
called_process_error : sp.CalledProcessError | ||
Source subprocess.CalledError | ||
""" | ||
self.returncode = called_process_error.returncode | ||
self.cmd = called_process_error.cmd[0] | ||
self.stdout = called_process_error.stdout.decode() | ||
self.stderr = called_process_error.stderr.decode() | ||
self.output = called_process_error.output | ||
|
||
def __str__(self): | ||
"""Improved string representation of the error message. | ||
Returns | ||
------- | ||
str | ||
Improved error messaging | ||
""" | ||
return f"Error {self.returncode}: {self.stderr}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
"""Tests for utilities.py""" | ||
|
||
import hpcpy.utilities as hu | ||
import hpcpy.exceptions as hx | ||
|
||
|
||
def test_interpolate_string_template(): | ||
"""Test interpolating a string template.""" | ||
assert hu.interpolate_string_template("hello {{arg}}", arg="world") == "hello world" | ||
|
||
|
||
def test_shell_exception(): | ||
"""Test that error messaging is being raised to the user.""" | ||
try: | ||
hu.shell("blah") | ||
except hx.ShellException as ex: | ||
assert ex.__str__() == "Error 127: /bin/sh: blah: command not found\n" |