Handle operation failure in api.

master
blallo 2019-08-01 00:28:09 +02:00 committed by blallo
parent 5e906d53cf
commit 9abf1855d9
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 25 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
class OperationFailed(RuntimeError):
pass