diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-04-02 12:51:34 +0200 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-04-03 13:31:10 +0200 |
commit | dfde363aa63ad3e7967da49f4ab599399b89e7f8 (patch) | |
tree | 051f1f1d1e3c8e07ae12af2bfb35174be18f1201 /src/utils/legacy.py | |
parent | c5ccc3c7e201beccf8eb3d329e478bb33b34d43e (diff) |
src: log backtrace in unhandled error cases
Log an error message in known error cases and log a backtrace
otherwise.
Define a new error type OgError to be used in all the 'raise'
blocks to define the error message to log. The exception
propagates until it reaches send_internal_server_error() where
the exception type is checked. If the type is OgError we log
the exception message. Logs the backtrace for other types.
The initial error implementation printed a backtrace everytime
an error ocurred. The next iteration changed it to only print
a backtrace in a very particular case but ended up omiting too
much information such as syntax errors or unknown error context.
The actual implementation only logs the cases we already cover in
the codebase and logs a bracktrace in the others, enabling a
better debugging experience.
Diffstat (limited to 'src/utils/legacy.py')
-rw-r--r-- | src/utils/legacy.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/utils/legacy.py b/src/utils/legacy.py index e740450..cdc79c9 100644 --- a/src/utils/legacy.py +++ b/src/utils/legacy.py @@ -16,6 +16,7 @@ import shutil from subprocess import PIPE, DEVNULL, STDOUT, CalledProcessError from src.utils.fs import umount +from src.log import OgError class ImageInfo: @@ -75,9 +76,9 @@ def image_info_from_partclone(partclone_output): fill_imageinfo(line, image_info) if not image_info.datasize: - raise ValueError("Missing device size from partclone.info output") + raise OgError("Missing device size from partclone.info output") elif not image_info.filesystem: - raise ValueError("Missing filesystem from partclone.info output") + raise OgError("Missing filesystem from partclone.info output") return image_info @@ -100,7 +101,7 @@ def run_lzop_partcloneinfo(image_path): p2_out, p2_err = p2.communicate() if p2.returncode != 0: - raise ValueError(f'Unable to process image {image_path}') + raise OgError(f'Unable to process image {image_path}') return p2_out @@ -174,7 +175,7 @@ def ogChangeRepo(ip, smb_user='opengnsys', smb_pass='og'): try: ipaddr = ipaddress.ip_address(ip) except ValueError as e: - raise ValueError(f'Invalid IP address {ip} received') + raise OgError(f'Invalid IP address {ip} received') from e mounted = False with open('/etc/mtab') as f: @@ -209,7 +210,7 @@ def restoreImageCustom(repo_ip, image_name, disk, partition, method): """ """ if not shutil.which('restoreImageCustom'): - raise OSError('restoreImageCustom not found') + raise OgError('restoreImageCustom not found') cmd = f'restoreImageCustom {repo_ip} {image_name} {disk} {partition} {method}' with open('/tmp/command.log', 'wb', 0) as logfile: @@ -220,7 +221,7 @@ def restoreImageCustom(repo_ip, image_name, disk, partition, method): shell=True, check=True) except OSError as e: - raise OSError(f'Error processing restoreImageCustom: {e}') from e + raise OgError(f'Error processing restoreImageCustom: {e}') from e return proc.returncode @@ -240,6 +241,6 @@ def configureOs(disk, partition): check=True) out = proc.stdout except OSError as e: - raise OSError(f'Error processing configureOsCustom: {e}') from e + raise OgError(f'Error processing configureOsCustom: {e}') from e return out |