Poor error handling in qemu_img helper
nirs opened this issue · 0 comments
nirs commented
This module started as a testing helper, and fort his purpose is fine. But now it is used in
user visible context (e.g. client.upload, clinet.checksum) and it needs more robust error
handling.
One example is running client.checksum() with missing file:
$ examples/checksum no-such-file.qcow2
qemu-img: Could not open 'no-such-file.qcow2': Could not open 'no-such-file.qcow2': No such file or directory
Traceback (most recent call last):
File "/home/nsoffer/src/ovirt-imageio/examples/checksum", line 17, in <module>
res = client.checksum(path, algorithm="sha256")
File "/usr/lib64/python3.10/site-packages/ovirt_imageio/client/_api.py", line 245, in checksum
image_info = info(filename, member=member)
File "/usr/lib64/python3.10/site-packages/ovirt_imageio/client/_api.py", line 205, in info
return qemu_img.info(filename)
File "/usr/lib64/python3.10/site-packages/ovirt_imageio/_internal/qemu_img.py", line 61, in info
out = subprocess.check_output(cmd)
File "/usr/lib64/python3.10/subprocess.py", line 420, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/lib64/python3.10/subprocess.py", line 524, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['qemu-img', 'info', '--output', 'json', 'no-such-file.qcow2']' returned non-zero exit status 1.
We get a good error from qemu-img written to stderr, but the traceback is not helpful.
Fix:
- Capture stderr when running commands
- Include stderr in the traceback (subprocess.CalledProcessError may already do the right thing)