From 2a4ce65a20b41a670b274cb473d46e62b4f3c913 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Mon, 18 Mar 2024 14:17:12 +0100 Subject: src: centralize error logging into send_internal_server_error Use only the exception messages as the main resource for error messages. The previous error code had string duplication in the form of: logging.error('msg here') raise Exception('msg here') That approach also has the downside of having log duplication as it had the local logging.err() and a global logging.exception() inside send_internal_server_error capturing the exception message. The actual code only requires raising an exception with a proper error message. Improve exception messages to give more error context. Log every AssertionError as a backtrace. Use the 'raise Exception from e' syntax to modify the a previously raised exception 'e' into an exception with aditional context or different type. This also prevents the message that warns about newer exceptions being launch after an initial exception. --- src/utils/boot.py | 5 ++--- src/utils/fs.py | 12 ++++-------- src/utils/legacy.py | 27 +++++++++++++-------------- src/utils/tiptorrent.py | 20 +++++++------------- src/utils/uefi.py | 5 ++--- 5 files changed, 28 insertions(+), 41 deletions(-) (limited to 'src/utils') diff --git a/src/utils/boot.py b/src/utils/boot.py index a5ff04b..c9b12ee 100644 --- a/src/utils/boot.py +++ b/src/utils/boot.py @@ -38,9 +38,8 @@ def _boot_bios_linux(disk, part, mountpoint): try: subprocess.run(shlex.split(kexec_cmd), check=True, text=True) subprocess.run(shlex.split(kexec_reboot_cmd), check=True, text=True) - except subprocess.CalledProcessError as e: - logging.error(f'Error processing kexec: {e}') - raise e + except OSError as e: + raise OSError(f'Error processing kexec: {e}') from e def _boot_bios_windows(disk, part, mountpoint): logging.info(f'Booting Windows system') diff --git a/src/utils/fs.py b/src/utils/fs.py index 69b4789..1015754 100644 --- a/src/utils/fs.py +++ b/src/utils/fs.py @@ -150,14 +150,12 @@ def mkfs(fs, disk, partition, label=None): } if fs not in fsdict: - logging.error(f'mkfs fails, unsupported target filesystem {fs}') - raise ValueError(f'Unsupported target filesystem {fs}') + raise ValueError(f'mkfs failed, unsupported target filesystem {fs}') try: partdev = get_partition_device(disk, partition) except ValueError as e: - logging.error(f'mkfs aborted, invalid partition.') - raise e + raise ValueError(f'mkfs aborted: {e}') from e fsdict[fs](partdev, label) @@ -253,13 +251,11 @@ def _reduce_ntfsresize(partdev): data_split = output_data.split(pattern) # If we fail to match pattern in the split then data_split will contain [output_data] if len(data_split) == 1: - logging.error(f'nfsresize: failed to find: "{pattern}"') - raise ValueError(f'nfsresize: failed to find: "{pattern}"') + raise ValueError(f'nfsresize: failed to find: {pattern}') value_str = data_split[1].split(' ')[0] if not value_str.isdigit() or value_str.startswith('-'): - logging.error(f'nfsresize: failed to parse numeric value at "{pattern}"') - raise ValueError(f'nfsresize: failed to parse numeric value at "{pattern}"') + raise ValueError(f'nfsresize: failed to parse numeric value at {pattern}') return int(value_str) try: diff --git a/src/utils/legacy.py b/src/utils/legacy.py index 387f790..d3a0318 100644 --- a/src/utils/legacy.py +++ b/src/utils/legacy.py @@ -209,8 +209,7 @@ def restoreImageCustom(repo_ip, image_name, disk, partition, method): """ """ if not shutil.which('restoreImageCustom'): - logging.error('Invalid restoreImageCustom invocation') - raise ValueError('Error: restoreImageCustom not found') + raise OSError('restoreImageCustom not found') cmd = f'restoreImageCustom {repo_ip} {image_name} {disk} {partition} {method}' with open('/tmp/command.log', 'wb', 0) as logfile: @@ -218,10 +217,10 @@ def restoreImageCustom(repo_ip, image_name, disk, partition, method): proc = subprocess.run(cmd, stdout=logfile, encoding='utf-8', - shell=True) - except: - logging.error('Exception when running restoreImageCustom subprocess') - raise ValueError('Error: Incorrect command value') + shell=True, + check=True) + except OSError as e: + raise OSError(f'Error processing restoreImageCustom: {e}') from e return proc.returncode @@ -237,11 +236,11 @@ def configureOs(disk, partition): proc = subprocess.run(cmd_configure, stdout=PIPE, encoding='utf-8', - shell=True) + shell=True, + check=True) out = proc.stdout - except: - logging.error('Exception when running configureOs subprocess') - raise ValueError('Error: Incorrect command value') + except OSError as e: + raise OSError(f'Error processing configureOsCustom: {e}') from e return out @@ -250,7 +249,7 @@ def ogCopyEfiBootLoader(disk, partition): cmd = f'ogCopyEfiBootLoader {disk} {partition}' try: proc = subprocess.run(cmd, - shell=True) - except: - logging.error('Exception when running ogCopyEfiBootLoader subprocess') - raise ValueError('Subprocess error: ogCopyEfiBootloader') + shell=True, + check=True) + except OSError as e: + raise OSError(f'Error processing ogCopyEfiBootLoader: {e}') from e diff --git a/src/utils/tiptorrent.py b/src/utils/tiptorrent.py index d46b1c7..18c4df7 100644 --- a/src/utils/tiptorrent.py +++ b/src/utils/tiptorrent.py @@ -33,11 +33,9 @@ def tip_fetch_csum(tip_addr, image_name): with urllib.request.urlopen(f'{url}') as resp: r = resp.readline().rstrip().decode('utf-8') except urllib.error.URLError as e: - logging.warning('URL error when fetching checksum: {e.reason}') - raise e + raise urllib.error.URLError(f'URL error when fetching checksum: {e.reason}') from e except urllib.error.HTTPError as e: - logging.warning(f'HTTP Error when fetching checksum: {e.reason}') - raise e + raise urllib.error.URLError(f'HTTP Error when fetching checksum: {e.reason}') from e return r @@ -48,8 +46,7 @@ def tip_write_csum(image_name): image_path = f'/opt/opengnsys/cache/opt/opengnsys/images/{image_name}.img' if not os.path.exists(image_path): - logging.error('Invalid image path') - raise ValueError('Invalid image path for tiptorrent checksum writing') + raise RuntimeError(f'Invalid image path {image_path} for tiptorrent checksum writing') filename = image_path + ".full.sum" csum = _compute_md5(image_path) @@ -65,8 +62,7 @@ def tip_check_csum(tip_addr, image_name): logging.info(f'Verifying checksum for {image_name}.img, please wait...') image_path = f'/opt/opengnsys/cache/opt/opengnsys/images/{image_name}.img' if not os.path.exists(image_path): - logging.error('Invalid image path') - raise ValueError('Invalid image path for tiptorrent image csum comparison') + raise RuntimeError(f'Invalid image path {image_path} for tiptorrent image csum comparison') cache_csum = _compute_md5(image_path) remote_csum = tip_fetch_csum(tip_addr, image_name) @@ -88,15 +84,13 @@ def tip_client_get(tip_addr, image_name): stdout=logfile, cwd='/opt/opengnsys/cache/opt/opengnsys/images/') proc.communicate() - except: - logging.exception('Exception when running tiptorrent client GET subprocess') - raise ValueError('Unexpected error running tiptorrent subprocess') + except OSError as e: + raise OSError('Unexpected error running tiptorrent subprocess: {e}') from e finally: logfile.close() if proc.returncode != 0: - logging.error(f'Error fetching image {image_name} via tiptorrent') - raise ValueError('Tiptorrent download failed') + raise RuntimeError(f'Error fetching image {image_name} via tiptorrent') else: logging.info('Calculating checksum...') logging.info('*DO NOT REBOOT OR POWEROFF* the client during this time') diff --git a/src/utils/uefi.py b/src/utils/uefi.py index 67f1354..e113512 100644 --- a/src/utils/uefi.py +++ b/src/utils/uefi.py @@ -104,6 +104,5 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True logging.info(f'{EFIBOOTMGR_BIN} command creating boot entry: {efibootmgr_cmd}') try: proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True) - except subprocess.CalledProcessError as e: - logging.error(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy?.') - raise e + except OSError as e: + raise OSError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e -- cgit v1.2.3-18-g5258