summaryrefslogtreecommitdiffstats
path: root/src/utils/fs.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/fs.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/fs.py')
-rw-r--r--src/utils/fs.py13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/utils/fs.py b/src/utils/fs.py
index 7e1d115..83d8d47 100644
--- a/src/utils/fs.py
+++ b/src/utils/fs.py
@@ -10,6 +10,7 @@ import logging
import os
import subprocess
import shlex
+from src.log import OgError
from subprocess import DEVNULL, PIPE, STDOUT
@@ -148,12 +149,12 @@ def mkfs(fs, disk, partition, label=None):
}
if fs not in fsdict:
- raise ValueError(f'mkfs failed, unsupported target filesystem {fs}')
+ raise OgError(f'mkfs failed, unsupported target filesystem {fs}')
try:
partdev = get_partition_device(disk, partition)
except ValueError as e:
- raise ValueError(f'mkfs aborted: {e}') from e
+ raise OgError(f'mkfs aborted: {e}') from e
fsdict[fs](partdev, label)
@@ -249,11 +250,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:
- raise ValueError(f'nfsresize: failed to find: {pattern}')
+ raise OgError(f'nfsresize: failed to find: {pattern}')
value_str = data_split[1].split(' ')[0]
if not value_str.isdigit() or value_str.startswith('-'):
- raise ValueError(f'nfsresize: failed to parse numeric value at {pattern}')
+ raise OgError(f'nfsresize: failed to parse numeric value at {pattern}')
return int(value_str)
try:
@@ -307,11 +308,11 @@ def _extend_resize2fs(partdev):
cmd = shlex.split(f'resize2fs -f {partdev}')
proc = subprocess.run(cmd)
if proc.returncode != 0:
- raise RuntimeError(f'Error growing ext4 filesystem at {partdev}')
+ raise OgError(f'Error growing ext4 filesystem at {partdev}')
def _extend_ntfsresize(partdev):
cmd = shlex.split(f'ntfsresize -f {partdev}')
proc = subprocess.run(cmd, input=b'y')
if proc.returncode != 0:
- raise RuntimeError(f'Error growing ntfs filesystem at {partdev}')
+ raise OgError(f'Error growing ntfs filesystem at {partdev}')