From dfde363aa63ad3e7967da49f4ab599399b89e7f8 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 2 Apr 2024 12:51:34 +0200 Subject: 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. --- src/ogClient.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/ogClient.py') diff --git a/src/ogClient.py b/src/ogClient.py index abacc12..c30070c 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -16,6 +16,7 @@ from io import StringIO from src.restRequest import * from src.ogRest import * +from src.log import OgError from enum import Enum class State(Enum): @@ -31,7 +32,7 @@ class ogClient: self.mode = self.CONFIG['opengnsys']['mode'] if self.mode not in {'virtual', 'live', 'linux', 'windows'}: - raise ValueError(f'Invalid ogClient mode: {self.mode}.') + raise OgError(f'Invalid ogClient mode: {self.mode}.') if self.mode in {'linux', 'windows'}: self.event_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.event_sock.setblocking(0) @@ -190,4 +191,4 @@ class ogClient: message = event_sock.recv(4096).decode('utf-8').rstrip() self.send_event_hint(message) else: - raise ValueError(f'Invalid ogClient run state: {str(state)}.') + raise OgError(f'Invalid ogClient run state: {str(state)}.') -- cgit v1.2.3-18-g5258