diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-03-18 14:17:12 +0100 |
---|---|---|
committer | lupoDharkael <izhe@hotmail.es> | 2024-03-21 10:29:57 +0100 |
commit | 2a4ce65a20b41a670b274cb473d46e62b4f3c913 (patch) | |
tree | 947c45ef966b226cbb980beabdc964184aa81a02 /src/utils/tiptorrent.py | |
parent | 0cbf16461e05bc5f15d3436763667b1d34869826 (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/tiptorrent.py')
-rw-r--r-- | src/utils/tiptorrent.py | 20 |
1 files changed, 7 insertions, 13 deletions
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') |