refactor: use try block for error handling and session rollback
This commit was merged in pull request #34.
This commit is contained in:
@@ -60,6 +60,7 @@ 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()
|
||||||
|
try:
|
||||||
if (data):
|
if (data):
|
||||||
ws_data = WebSocketData.model_validate(json.loads(data))
|
ws_data = WebSocketData.model_validate(json.loads(data))
|
||||||
session = SessionLocal()
|
session = SessionLocal()
|
||||||
@@ -70,9 +71,12 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
|
|||||||
).returning(processed_agent_data)
|
).returning(processed_agent_data)
|
||||||
res = session.execute(update_query).fetchone()
|
res = session.execute(update_query).fetchone()
|
||||||
if (not res):
|
if (not res):
|
||||||
print("Websocket update fail")
|
session.rollback()
|
||||||
|
raise Exception("Error while websocket PUT")
|
||||||
session.commit()
|
session.commit()
|
||||||
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
subscriptions.remove(websocket)
|
subscriptions.remove(websocket)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user