Logging tidy-up, IDE inspection fixes

This commit is contained in:
Ian Renton
2026-08-15 07:42:38 +01:00
parent 9cd6d9c177
commit bff5b79f8f
40 changed files with 128 additions and 125 deletions
+6 -17
View File
@@ -1,5 +1,4 @@
import copy
import inspect
import logging
from datetime import datetime
from typing import Any
@@ -53,28 +52,18 @@ class APIAlertsHandler(tornado.web.RequestHandler):
data = get_alert_list_with_filters(self._alerts, query_params)
if credentials:
data = self._enrich(data, credentials)
find_bad_values(data)
self.write(safe_json_dumps(data))
self.set_status(200)
except ValueError as e:
self.write(safe_json_dumps("Bad request - " + str(e)))
self.set_status(400)
except Exception as e:
logging.error("Exception when handling client request to alerts API: %s", e, exc_info=True)
except Exception:
logging.exception("Exception when handling client request to alerts API")
self.write(safe_json_dumps("Error - an internal server error occurred."))
self.set_status(500)
self.set_header("Cache-Control", "no-store")
self.set_header("Content-Type", "application/json")
def find_bad_values(obj, path="data"):
if isinstance(obj, dict):
for k, v in obj.items():
find_bad_values(v, f"{path}[{k!r}]")
elif isinstance(obj, (list, tuple)):
for i, v in enumerate(obj):
find_bad_values(v, f"{path}[{i}]")
elif inspect.isbuiltin(obj) or inspect.ismethod(obj) or inspect.isfunction(obj):
print(f"Found bad value at {path}: {obj!r}")
class APIAlertsStreamHandler(tornado_eventsource.handler.EventSourceHandler):
"""API request handler for /api/v2/alerts/stream"""
@@ -116,8 +105,8 @@ class APIAlertsStreamHandler(tornado_eventsource.handler.EventSourceHandler):
# argument.
self._sse_alert_broadcaster.register(self)
except Exception as e:
logging.warning("Exception when serving SSE socket: %s", e, exc_info=True)
except Exception:
logging.exception("Exception when serving SSE socket")
self.close()
def close(self):
@@ -135,8 +124,8 @@ class APIAlertsStreamHandler(tornado_eventsource.handler.EventSourceHandler):
alert = copy.deepcopy(alert)
alert.infer_missing(self._credentials)
self.write_message(msg=safe_json_dumps(alert))
except Exception as e:
logging.warning("Exception in SSE callback, connection will be closed: %s", e, exc_info=True)
except Exception:
logging.exception("Exception in SSE callback, connection will be closed")
self.close()