diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-04-02 12:51:34 +0200 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-04-03 13:31:10 +0200 |
commit | dfde363aa63ad3e7967da49f4ab599399b89e7f8 (patch) | |
tree | 051f1f1d1e3c8e07ae12af2bfb35174be18f1201 /src/virtual | |
parent | c5ccc3c7e201beccf8eb3d329e478bb33b34d43e (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/virtual')
-rw-r--r-- | src/virtual/ogOperations.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/virtual/ogOperations.py b/src/virtual/ogOperations.py index 0819a76..3cfe83a 100644 --- a/src/virtual/ogOperations.py +++ b/src/virtual/ogOperations.py @@ -23,6 +23,7 @@ import math import sys import enum import time +from src.log import OgError class OgVM: DEFAULT_CPU = 'host' @@ -97,7 +98,7 @@ class OgQMP: self.sock.connect((self.ip, self.port)) except socket.error as err: if err.errno == errno.ECONNREFUSED: - raise RuntimeError('Cannot connect to QEMU') + raise OgError('Cannot connect to QEMU') elif err.errno == errno.EINPROGRESS: pass @@ -114,11 +115,11 @@ class OgQMP: pass if 'QMP' not in out: - raise RuntimeError('Cannot handshake QEMU') + raise OgError('Cannot handshake QEMU') out = self.talk(str({"execute": "qmp_capabilities"})) if 'return' not in out: - raise RuntimeError('Cannot handshake QEMU') + raise OgError('Cannot handshake QEMU') def disconnect(self): try: @@ -143,9 +144,9 @@ class OgQMP: try: self.sock.send(bytes(data, 'utf-8')) except Exception as e: - raise RuntimeError('Cannot talk to QEMU') from e + raise OgError('Cannot talk to QEMU') from e else: - raise RuntimeError('Timeout when talking to QEMU') + raise OgError('Timeout when talking to QEMU') return self.recv(timeout=timeout) @@ -158,9 +159,9 @@ class OgQMP: out = self.sock.recv(4096).decode('utf-8') out = json.loads(out) except socket.error as err: - raise RuntimeError('Cannot talk to QEMU') from err + raise OgError('Cannot talk to QEMU') from err else: - raise RuntimeError('Timeout when talking to QEMU') + raise OgError('Timeout when talking to QEMU') return out class OgVirtualOperations: |