Skip to content

Commit

Permalink
Merge pull request #21 from hsahovic/explicit-exception-handling
Browse files Browse the repository at this point in the history
Explicit exception handling
  • Loading branch information
hsahovic authored Mar 6, 2020
2 parents 9f0bf57 + 9c8022a commit e92bdb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ asynctest
black
flake8
pre-commit
pyre-check==0.0.43
pyre-check==0.0.41
pytest
pytest-asyncio
pytest-cov
Expand Down
19 changes: 18 additions & 1 deletion src/poke_env/player/player_network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from asyncio import ensure_future
from asyncio import Event
from asyncio import Lock
from asyncio import sleep
from time import perf_counter
from typing import List
from typing import Optional

Expand Down Expand Up @@ -82,7 +84,7 @@ async def _change_avatar(self, avatar_id: Optional[int]) -> None:
:param avatar_id: The new avatar id. If None, nothing happens.
:type avatar_id: int
"""
assert self.logged_in.is_set()
self._wait_for_login()
if avatar_id is not None:
await self._send_message(f"/avatar {avatar_id}")

Expand Down Expand Up @@ -159,6 +161,11 @@ async def _handle_message(self, message: str) -> None:
raise NotImplementedError("Unhandled message: %s" % message)
except CancelledError as e:
self.logger.critical("CancelledError intercepted. %s", e)
except Exception as exception:
self.logger.exception(
"Unhandled exception raised while handling message:\n%s", message
)
raise exception

async def _log_in(self, split_message: List[str]) -> None:
"""Log the player with specified username and password.
Expand Down Expand Up @@ -211,6 +218,16 @@ async def _send_message(
await self._websocket.send(to_send)
self.logger.info(">>> %s", to_send)

def _wait_for_login(
self, checking_interval: float = 0.001, wait_for: int = 5
) -> None:
start = perf_counter()
while perf_counter() - start < wait_for:
sleep(checking_interval)
if self.logged_in:
return
assert self.logged_in

async def listen(self) -> None:
"""Listen to a showdown websocket and dispatch messages to be handled."""
self.logger.info("Starting listening to showdown websocket")
Expand Down

0 comments on commit e92bdb9

Please sign in to comment.