From c10f933ca5624ce367f2a584a417ed98a02c6b09 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sat, 11 Nov 2017 12:38:11 +0200 Subject: [PATCH] Add util: safety execute coroutine --- aiogram/utils/safe.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 aiogram/utils/safe.py diff --git a/aiogram/utils/safe.py b/aiogram/utils/safe.py new file mode 100644 index 00000000..7ef5b8fc --- /dev/null +++ b/aiogram/utils/safe.py @@ -0,0 +1,16 @@ +import typing + + +async def safe(coro: typing.Coroutine) -> (bool, typing.Any): + """ + Safety execute coroutine + + Status - returns True if success otherwise False + + :param coro: + :return: status and result + """ + try: + return True, await coro + except Exception as e: + return False, e