Skip to content

Commit

Permalink
fix: update error handling in async_client.py and client.py
Browse files Browse the repository at this point in the history
  • Loading branch information
roerohan committed Apr 5, 2024
1 parent 2ca6dbd commit 4f78e8e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion deepgram/clients/live/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,12 @@ async def _signal_exit(self) -> None:
try:
# if the socket connection is closed, the following line might throw an error
await self._socket.send(json.dumps({"type": "CloseStream"}))
except websockets.exceptions.ConnectionClosedOK as e:
self.logger.notice(f"_signal_exit - connection closed: {e.code}")
except websockets.exceptions.WebSocketException as e:
self.logger.error(f"_signal_exit - WebSocketException: {str(e)}")
except Exception as e:
self.logger.error("Exception in AsyncLiveClient._signal_exit, %s", e)
self.logger.error(f"_signal_exit - Exception: {str(e)}")

await asyncio.sleep(0.5)

Expand Down
10 changes: 9 additions & 1 deletion deepgram/clients/live/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,15 @@ def _signal_exit(self) -> None:
self.logger.notice("closing socket...")
if self._socket is not None:
self.logger.notice("sending CloseStream...")
self.send(json.dumps({"type": "CloseStream"}))
try:
# if the socket connection is closed, the following line might throw an error
self._socket.send(json.dumps({"type": "CloseStream"}))
except websockets.exceptions.ConnectionClosedOK as e:
self.logger.notice(f"_signal_exit - connection closed: {e.code}")
except websockets.exceptions.WebSocketException as e:
self.logger.error(f"_signal_exit - WebSocketException: {str(e)}")
except Exception as e:
self.logger.error(f"_signal_exit - Exception: {str(e)}")

time.sleep(0.5)

Expand Down

0 comments on commit 4f78e8e

Please sign in to comment.