-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.py
48 lines (33 loc) · 1.5 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import asyncio
import os
from typing import Optional, Union
from aiogram import types
from aiokilogram.bot import KiloBot
from aiokilogram.settings import BaseGlobalSettings
from aiokilogram.handler import CommandHandler
from aiokilogram.errors import DefaultErrorHandler
from aiokilogram.page import MessagePage, simple_page
from aiokilogram.registration import register_message_handler
class MainErrorHandler(DefaultErrorHandler):
def make_message(self, err: Exception) -> Optional[Union[MessagePage, str]]:
return f'This is the main error message about {type(err).__name__}'
class CustomErrorHandler(DefaultErrorHandler):
def make_message(self, err: Exception) -> Optional[Union[MessagePage, str]]:
return simple_page(text=f'This is the custom error message about {type(err).__name__}')
class ErrorHandlingCommandHandler(CommandHandler):
"""Handles errors raised by methods"""
error_handler = MainErrorHandler()
@register_message_handler(commands={'main'})
async def main_error_raiser(self, event: types.Message) -> None:
raise RuntimeError
@register_message_handler(commands={'custom'}, error_handler=CustomErrorHandler())
async def custom_error_raiser(self, event: types.Message) -> None:
raise RuntimeError
def run_bot():
bot = KiloBot(
global_settings=BaseGlobalSettings(tg_bot_token=os.environ['TG_BOT_TOKEN']),
handler_classes=[ErrorHandlingCommandHandler],
)
asyncio.run(bot.run())
if __name__ == '__main__':
run_bot()