summaryrefslogtreecommitdiffstats
path: root/src/utils/legacy.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-03-18 14:17:12 +0100
committerlupoDharkael <izhe@hotmail.es>2024-03-21 10:29:57 +0100
commit2a4ce65a20b41a670b274cb473d46e62b4f3c913 (patch)
tree947c45ef966b226cbb980beabdc964184aa81a02 /src/utils/legacy.py
parent0cbf16461e05bc5f15d3436763667b1d34869826 (diff)
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.
Diffstat (limited to 'src/utils/legacy.py')
-rw-r--r--src/utils/legacy.py27
1 files changed, 13 insertions, 14 deletions
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