'Python3 replacing Python errors with different output
I have one issue that I would like fixed but I am not able to. I have a small script that requires SSH key to work properly. When the SSH key is not properly loaded, the following Python Error appears:
SSH: Permission denied (publickey). Traceback (most recent call last): File "/path/to/python3file.py", line 117, in func.func_check() File "/path/to/python3file.py", line 18, in func_check ssh = subprocess.check_output(["ssh", "-p22", "{}@{}".format("user", self.host), command]) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 411, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 512, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['ssh', '-p22', 'user@chost', ' script func user']' returned non-zero exit status 255.
Basically, I want to replace the entire error code from the above with something more user-friendly like:
Please import SSH key
Is that even possible?
Thank you.
Solution 1:[1]
Use Try Except, and specify the type of error you are handling. In this case it is IOError.
try:
print('my code here')
except IOError, e:
print('Please import SSH key')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | owen |
