From dfde363aa63ad3e7967da49f4ab599399b89e7f8 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 2 Apr 2024 12:51:34 +0200 Subject: 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. --- src/utils/uefi.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/utils/uefi.py') diff --git a/src/utils/uefi.py b/src/utils/uefi.py index b51d765..17b35cb 100644 --- a/src/utils/uefi.py +++ b/src/utils/uefi.py @@ -15,6 +15,7 @@ import shutil from src.utils.disk import * from src.utils.fs import * from src.utils.probe import * +from src.log import OgError import fdisk @@ -25,14 +26,14 @@ def _find_bootentry(entries, label): if entry['description'] == label: return entry else: - raise NameError('Boot entry {label} not found') + raise OgError('Boot entry {label} not found') def _strip_boot_prefix(entry): try: num = entry['name'][4:] except: - raise KeyError('Unable to strip "Boot" prefix from boot entry') + raise OgError('Unable to strip "Boot" prefix from boot entry') return num @@ -56,7 +57,7 @@ def is_uefi_supported(): def run_efibootmgr_json(): if _check_efibootmgr_json() is False: - raise RuntimeError(f'{EFIBOOTMGR_BIN} not available') + raise OgError(f'{EFIBOOTMGR_BIN} not available') proc = subprocess.run([EFIBOOTMGR_BIN, '--json'], capture_output=True, text=True) dict_json = json.loads(proc.stdout) @@ -99,14 +100,14 @@ def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True try: proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True) except OSError as e: - raise OSError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e + raise OgError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e def copy_windows_efi_bootloader(disk, partition): device = get_partition_device(disk, partition) mountpoint = device.replace('dev', 'mnt') if not mount_mkdir(device, mountpoint): - raise RuntimeError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}') + raise OgError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}') os_family = get_os_family(mountpoint) is_uefi = is_uefi_supported() @@ -119,7 +120,7 @@ def copy_windows_efi_bootloader(disk, partition): esp_mountpoint = esp.replace('dev', 'mnt') if not mount_mkdir(esp, esp_mountpoint): umount(mountpoint) - raise RuntimeError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}') + raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}') loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/bootmgfw.efi', f'{esp_mountpoint}/EFI/Microsoft/Boot/bootmgfw.efi'] @@ -131,7 +132,7 @@ def copy_windows_efi_bootloader(disk, partition): logging.info(f'Found bootloader at ESP partition: {loader}') break else: - raise RuntimeError(f'Unable to locate Windows EFI bootloader bootmgfw.efi') + raise OgError(f'Unable to locate Windows EFI bootloader bootmgfw.efi') loader_dir = os.path.dirname(loader) destination_dir = f'{mountpoint}/ogBoot' @@ -139,12 +140,12 @@ def copy_windows_efi_bootloader(disk, partition): try: shutil.rmtree(destination_dir) except Exception as e: - raise OSError(f'Failed to delete {destination_dir}: {e}') from e + raise OgError(f'Failed to delete {destination_dir}: {e}') from e logging.info(f'Copying {loader_dir} into {destination_dir}') try: shutil.copytree(loader_dir, destination_dir) except Exception as e: - raise OSError(f'Failed to copy {loader_dir} into {destination_dir}: {e}') from e + raise OgError(f'Failed to copy {loader_dir} into {destination_dir}: {e}') from e finally: umount(mountpoint) umount(esp_mountpoint) -- cgit v1.2.3-18-g5258