From 97e753cbfdfbaaaa027ed1a161e0ad0bf4960ba5 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Thu, 9 Mar 2023 16:04:18 +0100 Subject: ogRest: improve error logging when executing operations Capture all possible Python exceptions in the try/except block of every opengnsys operation. Create an error handling function to deduplicate code in the except block. The error handling function resets the ogRest state to IDLE and sends the corresponding 500 Internal Server Error. This *does not cover* every possible error. There are functions inside ogThread which contain code that may raise errors that are not covered by any try/except block. --- src/ogRest.py | 55 +++++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/ogRest.py b/src/ogRest.py index 69620e6..fe6c0da 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -87,10 +87,8 @@ class ogThread(): try: shellout = ogRest.operations.shellrun(request, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return if request.getEcho(): @@ -114,10 +112,8 @@ class ogThread(): def session(client, request, ogRest): try: ogRest.operations.session(request, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return response = restResponse(ogResponses.OK) @@ -127,10 +123,8 @@ class ogThread(): def software(client, request, path, ogRest): try: software = ogRest.operations.software(request, path, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return json_body = jsonBody() @@ -144,10 +138,8 @@ class ogThread(): def hardware(client, path, ogRest): try: ogRest.operations.hardware(path, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return json_body = jsonBody() @@ -161,10 +153,8 @@ class ogThread(): def setup(client, request, ogRest): try: out = ogRest.operations.setup(request, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return json_body = jsonBody(out) @@ -176,10 +166,8 @@ class ogThread(): def image_restore(client, request, ogRest): try: ogRest.operations.image_restore(request, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return json_body = jsonBody() @@ -197,10 +185,8 @@ class ogThread(): request, ogRest) software = ogRest.operations.software(request, path, ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return kibi = 1024 @@ -226,10 +212,8 @@ class ogThread(): def refresh(client, ogRest): try: out = ogRest.operations.refresh(ogRest) - except ValueError as err: - response = restResponse(ogResponses.INTERNAL_ERR) - client.send(response.get()) - ogRest.state = ThreadState.IDLE + except Exception as e: + ogRest.send_internal_server_error(client, exc=e) return json_body = jsonBody(out) @@ -276,6 +260,13 @@ class ogRest(): else: raise ValueError('Mode not supported.') + def send_internal_server_error(self, client, exc=None): + if exc: + logging.exception('Unexpected error') + response = restResponse(ogResponses.INTERNAL_ERR) + client.send(response.get()) + self.state = ThreadState.IDLE + def process_request(self, request, client): method = request.get_method() URI = request.get_uri() -- cgit v1.2.3-18-g5258