From 9abf1855d9324b16e66bcffaf07643dd5e259259 Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 1 Aug 2019 00:28:09 +0200 Subject: [PATCH] Handle operation failure in api. --- api/async_bot.py | 21 +++++++++++++++++---- bot_z/async_operator.py | 10 ++++++---- bot_z/exceptions.py | 2 ++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 bot_z/exceptions.py diff --git a/api/async_bot.py b/api/async_bot.py index 38c2926..5a8afba 100644 --- a/api/async_bot.py +++ b/api/async_bot.py @@ -8,6 +8,7 @@ import asyncio from contextlib import contextmanager from bot_z.async_operator import AsyncOperator +from bot_z.exceptions import OperationFailed @contextmanager @@ -31,7 +32,10 @@ async def login(op: AsyncOperator, username: str, password: str) -> bool: the AsyncOperator object. """ if not op.logged_in: - await op.login(username, password) + try: + await op.login(username, password) + except OperationFailed: + pass return op.logged_in @@ -41,7 +45,10 @@ async def logout(op: AsyncOperator) -> bool: a bool showing the success or failure of the operation. """ if op.logged_in: - await op.logout() + try: + await op.logout() + except OperationFailed: + pass return op.logged_in @@ -51,7 +58,10 @@ async def checkin(op: AsyncOperator) -> bool: a bool showing the success or failure of the operation. """ if op.logged_in and not op.checked_in: - await op.checkin() + try: + await op.checkin() + except OperationFailed: + pass return op.checked_in @@ -61,5 +71,8 @@ async def checkout(op: AsyncOperator) -> bool: a bool showing the success or failure of the operation. """ if op.logged_in and op.checked_in: - await op.checkout() + try: + await op.checkout() + except OperationFailed: + pass return not op.checked_in diff --git a/bot_z/async_operator.py b/bot_z/async_operator.py index 559a6ec..5d2f95b 100644 --- a/bot_z/async_operator.py +++ b/bot_z/async_operator.py @@ -12,6 +12,7 @@ import functools import logging from bot_z.operator import Operator +from bot_z.exceptions import OperationFailed alog = logging.getLogger("asyncio") @@ -26,6 +27,7 @@ async def _push_to_loop(loop, executor, func, *args, **kwargs): return res.result() +# TODO: make it JSON-serializable class AsyncOperator(object): """ This is the async version of the Operator. @@ -47,7 +49,7 @@ class AsyncOperator(object): self.loop, self.executor, self.op.login, username, password ) if not self.op.logged_in: - raise RuntimeError("Failed to login.") + raise OperationFailed("Failed to login.") alog.info("Logged in [%s]", self.name) async def logout(self) -> None: @@ -55,7 +57,7 @@ class AsyncOperator(object): alog.debug("Logging out [%s]", self.name) _ = await _push_to_loop(self.loop, self.executor, self.op.logout) if self.op.logged_in: - raise RuntimeError("Failed to logout.") + raise OperationFailed("Failed to logout.") alog.info("Logged out [%s]", self.name) async def checkin(self) -> None: @@ -63,7 +65,7 @@ class AsyncOperator(object): alog.debug("Checking in [%s]", self.name) _ = await _push_to_loop(self.loop, self.executor, self.op.check_in) if not self.op.checked_in: - raise RuntimeError("Failed to checkin.") + raise OperationFailed("Failed to checkin.") alog.info("Checked in [%s]", self.name) async def checkout(self) -> None: @@ -71,7 +73,7 @@ class AsyncOperator(object): alog.debug("Checking out [%s]", self.name) _ = await _push_to_loop(self.loop, self.executor, self.op.check_out) if self.op.checked_in: - raise RuntimeError("Failed to checkout.") + raise OperationFailed("Failed to checkout.") alog.info("Checked out [%s]", self.name) @property diff --git a/bot_z/exceptions.py b/bot_z/exceptions.py new file mode 100644 index 0000000..df2dec0 --- /dev/null +++ b/bot_z/exceptions.py @@ -0,0 +1,2 @@ +class OperationFailed(RuntimeError): + pass