summaryrefslogtreecommitdiffstats
path: root/src/ogRest.py
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2023-03-09 16:04:18 +0100
committerJose M. Guisado <jguisado@soleta.eu>2023-03-10 11:26:46 +0100
commit97e753cbfdfbaaaa027ed1a161e0ad0bf4960ba5 (patch)
tree23e7d1110e9ee82653a5baf0f2fb67978f0cf2a6 /src/ogRest.py
parent5b5ef607eca3e5f72b597f75dae7e415ce117a9f (diff)
ogRest: improve error logging when executing operationsv1.2.6
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.
Diffstat (limited to 'src/ogRest.py')
-rw-r--r--src/ogRest.py55
1 files changed, 23 insertions, 32 deletions
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()