refactor: use try block for error handling and session rollback
All checks were successful
Component testing / Hub testing (push) Successful in 23s
Component testing / Store testing (push) Successful in 30s
Component testing / Integration smoke testing (push) Successful in 2m28s

This commit was merged in pull request #34.
This commit is contained in:
ІМ-24 Владислав Коваленко
2026-03-26 13:38:04 +00:00
parent 0d364ddf61
commit d073243c67

View File

@@ -60,19 +60,23 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
# receive forever # receive forever
while True: while True:
data = await websocket.receive_text() data = await websocket.receive_text()
if (data): try:
ws_data = WebSocketData.model_validate(json.loads(data)) if (data):
session = SessionLocal() ws_data = WebSocketData.model_validate(json.loads(data))
update_query = ( session = SessionLocal()
processed_agent_data.update() update_query = (
.where(processed_agent_data.c.id == ws_data.id) processed_agent_data.update()
.values(visible=False) .where(processed_agent_data.c.id == ws_data.id)
).returning(processed_agent_data) .values(visible=False)
res = session.execute(update_query).fetchone() ).returning(processed_agent_data)
if (not res): res = session.execute(update_query).fetchone()
print("Websocket update fail") if (not res):
session.commit() session.rollback()
raise Exception("Error while websocket PUT")
session.commit()
finally:
session.close() session.close()
except WebSocketDisconnect: except WebSocketDisconnect:
subscriptions.remove(websocket) subscriptions.remove(websocket)