Skip to content

Commit 2646342

Browse files
author
Alan Christie
committed
fix: Better logging
1 parent 9773477 commit 2646342

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

app/app.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def event_stream(websocket: WebSocket, uuid: str):
158158
routing_key,
159159
)
160160
await websocket.accept()
161-
_LOGGER.debug("Accepted connection for %s", es_id)
161+
_LOGGER.info("Accepted connection for %s", es_id)
162162

163163
_LOGGER.debug("Creating reader for %s...", es_id)
164164
message_reader = _get_from_queue(routing_key)
@@ -173,16 +173,16 @@ async def event_stream(websocket: WebSocket, uuid: str):
173173
message_body = await reader
174174
_LOGGER.debug("Got message for %s (message_body=%s)", es_id, message_body)
175175
if message_body == b"POISON":
176-
_LOGGER.debug("Taking POISON for %s (%s) (closing)...", es_id, uuid)
176+
_LOGGER.info("Taking POISON for %s (%s) (closing)...", es_id, uuid)
177177
_running = False
178178
else:
179179
await websocket.send_text(str(message_body))
180180

181-
_LOGGER.debug("Closing %s (uuid=%s)...", es_id, uuid)
181+
_LOGGER.info("Closing %s (uuid=%s)...", es_id, uuid)
182182
await websocket.close(
183183
code=status.WS_1000_NORMAL_CLOSURE, reason="The stream has been deleted"
184184
)
185-
_LOGGER.debug("Closed %s", es_id)
185+
_LOGGER.info("Closed %s", es_id)
186186

187187

188188
async def _get_from_queue(routing_key: str):
@@ -239,9 +239,16 @@ def post_es(request_body: EventStreamPostRequestBody) -> EventStreamPostResponse
239239
db.commit()
240240
# Now pull the record back to get the assigned record ID...
241241
es = cursor.execute(f"SELECT * FROM es WHERE uuid='{uuid_str}'").fetchone()
242-
assert es, f"Failed to insert new event stream {uuid_str}"
243242
db.close()
244-
243+
if not es:
244+
msg: str = (
245+
f"Failed to get new EventStream record ID for {uuid_str} (routing_key='{routing_key}')"
246+
)
247+
_LOGGER.error(msg)
248+
raise HTTPException(
249+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
250+
detail=msg,
251+
)
245252
_LOGGER.info("Created %s", es)
246253

247254
# And construct the location we'll be listening on...
@@ -285,9 +292,11 @@ def delete_es(es_id: int):
285292
es = cursor.execute(f"SELECT * FROM es WHERE id={es_id}").fetchone()
286293
db.close()
287294
if not es:
295+
msg: str = f"EventStream {es_id} is not known"
296+
_LOGGER.warning(msg)
288297
raise HTTPException(
289298
status_code=status.HTTP_404_NOT_FOUND,
290-
detail=f"EventStream {es_id} is not known",
299+
detail=msg,
291300
)
292301

293302
_LOGGER.info(

0 commit comments

Comments
 (0)