summaryrefslogtreecommitdiffstats
path: root/src/utils/hw_inventory.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/hw_inventory.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/hw_inventory.py')
-rw-r--r--src/utils/hw_inventory.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/utils/hw_inventory.py b/src/utils/hw_inventory.py
index c534101..7341dac 100644
--- a/src/utils/hw_inventory.py
+++ b/src/utils/hw_inventory.py
@@ -10,6 +10,7 @@ import json
import os.path
import shlex
import subprocess
+from src.log import OgError
from collections import namedtuple
from enum import Enum, auto
@@ -55,16 +56,16 @@ class HardwareInventory():
def add_element(self, elem):
if elem.type not in HardwareType:
- raise ValueError(f'Unsupported hardware type, received {elem.type}')
+ raise OgError(f'Unsupported hardware type, received {elem.type}')
if not elem.name:
- raise ValueError('Empty hardware element name')
+ raise OgError('Empty hardware element name')
self.elements.append(elem)
def _bytes_to_human(size):
suffixes = ['B', 'MiB', 'GiB', 'TiB']
if type(size) is not int:
- raise TypeError(f'Invalid type in _bytes_to_human, got: {size} {type(size)}')
+ raise OgError(f'Invalid type in _bytes_to_human, got: {size} {type(size)}')
for exponent, suffix in enumerate(suffixes, start=1):
conv = size / (1024**exponent)
if conv < 1024:
@@ -250,7 +251,7 @@ def legacy_hardware_element(element):
represented as "vga=Foo"
"""
if type(element) is not HardwareElement:
- raise TypeError('Invalid hardware element type')
+ raise OgError('Invalid hardware element type')
elif element.type is HardwareType.MULTIMEDIA:
nemonic = 'mul'
elif element.type is HardwareType.BOOTMODE:
@@ -297,7 +298,7 @@ def get_hardware_inventory():
if type(j) is list:
root = j[0]
if type(root) is not dict:
- raise ValueError('Invalid lshw json output')
+ raise OgError('Invalid lshw json output')
inventory = HardwareInventory()
_fill_computer_model(inventory, root)