summaryrefslogtreecommitdiffstats
path: root/src/utils/disk.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-04-02 12:51:34 +0200
committerAlejandro Sirgo Rica <asirgo@soleta.eu>2024-04-03 13:31:10 +0200
commitdfde363aa63ad3e7967da49f4ab599399b89e7f8 (patch)
tree051f1f1d1e3c8e07ae12af2bfb35174be18f1201 /src/utils/disk.py
parentc5ccc3c7e201beccf8eb3d329e478bb33b34d43e (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/disk.py')
-rw-r--r--src/utils/disk.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/utils/disk.py b/src/utils/disk.py
index 323994c..a44153d 100644
--- a/src/utils/disk.py
+++ b/src/utils/disk.py
@@ -8,6 +8,7 @@
import os
import logging
+from src.log import OgError
import fdisk
@@ -29,7 +30,7 @@ def get_partition_device(disknum, partnum):
"""
disk_index = disknum - 1
if disk_index < 0 or disk_index >= len(get_disks()):
- raise ValueError(f'Invalid disk number {disknum}, {len(get_disks())} disks available.')
+ raise OgError(f'Invalid disk number {disknum}, {len(get_disks())} disks available.')
disk = get_disks()[disk_index]
cxt = fdisk.Context(f'/dev/{disk}')
@@ -38,7 +39,7 @@ def get_partition_device(disknum, partnum):
if pa.partno == partnum - 1:
return cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE)
- raise ValueError(f'No such partition with disk index {disknum} and partition index {partnum}')
+ raise OgError(f'No such partition with disk index {disknum} and partition index {partnum}')
def get_efi_partition(disknum, enforce_gpt):
@@ -55,16 +56,16 @@ def get_efi_partition(disknum, enforce_gpt):
"""
disk_index = disknum - 1
if disk_index < 0 or disk_index >= len(get_disks()):
- raise ValueError(f'Invalid disk number {disknum} when trying to find ESP, {len(get_disks())} disks available.')
+ raise OgError(f'Invalid disk number {disknum} when trying to find ESP, {len(get_disks())} disks available.')
disk = get_disks()[disk_index]
cxt = fdisk.Context(f'/dev/{disk}')
if enforce_gpt and cxt.label == fdisk.FDISK_DISKLABEL_DOS:
- raise RuntimeError(f'Windows EFI System requires GPT partition scheme, but /dev/{disk} has DOS partition scheme')
+ raise OgError(f'Windows EFI System requires GPT partition scheme, but /dev/{disk} has DOS partition scheme')
for pa in cxt.partitions:
logging.info(f'Checking partition "{pa.type.name}"...')
if pa.type.name == 'EFI System':
return cxt.partition_to_string(pa, fdisk.FDISK_FIELD_DEVICE), f'/dev/{disk}', pa.partno + 1
- raise RuntimeError(f'Cannot find "EFI System" partition at /dev/{disk}')
+ raise OgError(f'Cannot find "EFI System" partition at /dev/{disk}')