diff --git a/bot/handlers/__init__.py b/bot/handlers/__init__.py index 10e2c72..c23c359 100644 --- a/bot/handlers/__init__.py +++ b/bot/handlers/__init__.py @@ -3,6 +3,7 @@ from . import ( initialize, inline_song, inline_url, + inline_error, inline_default, inline_empty, on_chosen, @@ -18,6 +19,7 @@ router.include_routers( initialize.router, inline_song.router, inline_url.router, + inline_error.router, inline_default.router, inline_empty.router, on_chosen.router, diff --git a/bot/handlers/inline_error/__init__.py b/bot/handlers/inline_error/__init__.py new file mode 100644 index 0000000..4c8559f --- /dev/null +++ b/bot/handlers/inline_error/__init__.py @@ -0,0 +1 @@ +from .on_inline_error_info import router diff --git a/bot/handlers/inline_error/on_inline_error_info.py b/bot/handlers/inline_error/on_inline_error_info.py new file mode 100644 index 0000000..2626e4f --- /dev/null +++ b/bot/handlers/inline_error/on_inline_error_info.py @@ -0,0 +1,17 @@ +from aiogram import Router + +from aiogram.types import InlineQuery + +from bot.results.error import get_error_search_results +from bot.filters import ServiceSearchFilter + +router = Router() + + +@router.inline_query(ServiceSearchFilter('error')) +async def search_spotify_inline_query(inline_query: InlineQuery): + await inline_query.answer( + await get_error_search_results(inline_query.query.removeprefix('error:')), + cache_time=0, + is_personal=True + ) diff --git a/bot/modules/error/__init__.py b/bot/modules/error/__init__.py index f00ed2a..f7c0612 100644 --- a/bot/modules/error/__init__.py +++ b/bot/modules/error/__init__.py @@ -1 +1,2 @@ -from .handler import on_error +from .handler import on_error, Error +from .pretty import PrettyException diff --git a/bot/modules/error/handler.py b/bot/modules/error/handler.py index bf5bc2a..93a481d 100644 --- a/bot/modules/error/handler.py +++ b/bot/modules/error/handler.py @@ -1,8 +1,10 @@ from bot.common import console from aiogram.types.error_event import ErrorEvent from aiogram import Bot +from aiogram.dispatcher import router as s_router from rich.traceback import Traceback +from .pretty import PrettyException from bot.modules.database import db @@ -11,6 +13,7 @@ from dataclasses import dataclass @dataclass class Error: + exception: PrettyException traceback: Traceback inline_message_id: str | None = None @@ -26,12 +29,14 @@ async def on_error(event: ErrorEvent, bot: Bot): event.exception, event.exception.__traceback__, show_locals=True, - max_frames=1, + suppress=[s_router], ) + pretty_exception = PrettyException(event.exception) if event.update.chosen_inline_result: db.errors[error_id] = Error( traceback=traceback, + exception=pretty_exception, inline_message_id=event.update.chosen_inline_result.inline_message_id, ) @@ -45,9 +50,10 @@ async def on_error(event: ErrorEvent, bot: Bot): else: db.errors[error_id] = Error( traceback=traceback, + exception=pretty_exception, ) console.print(f'[red]{error_id} occurred[/]') console.print(event) console.print(traceback) - console.print(f'-{error_id}-') + console.print(f'-{error_id} occurred-') diff --git a/bot/modules/error/pretty.py b/bot/modules/error/pretty.py index 13c6747..d4e3cfd 100644 --- a/bot/modules/error/pretty.py +++ b/bot/modules/error/pretty.py @@ -6,16 +6,18 @@ import re class PrettyException: def __init__(self, e: Exception): - self.pretty_exception = f""" -❌ Error! Report it to admins: -🐊 {e.__traceback__.tb_frame.f_code.co_filename.replace(os.getcwd(), "")}\r -:{e.__traceback__.tb_frame.f_lineno} + self.long = f""" 😍 {e.__class__.__name__} 👉 {"".join(traceback.format_exception_only(e)).strip()} - -⬇️ Trace: -{self.get_full_stack()} +🐊 {e.__traceback__.tb_frame.f_code.co_filename.replace(os.getcwd(), "")}\r +:{e.__traceback__.tb_frame.f_lineno} """ + self.short = (f'{e.__class__.__name__}: ' + f'{"".join(traceback.format_exception_only(e)).strip()}') + + self.pretty_exception = (f"{self.long}\n\n" + f"⬇️ Trace:" + f"{self.get_full_stack()}") @staticmethod def get_full_stack(): diff --git a/bot/results/error/__init__.py b/bot/results/error/__init__.py new file mode 100644 index 0000000..85a8743 --- /dev/null +++ b/bot/results/error/__init__.py @@ -0,0 +1 @@ +from .error import get_error_search_results diff --git a/bot/results/error/error.py b/bot/results/error/error.py new file mode 100644 index 0000000..4799792 --- /dev/null +++ b/bot/results/error/error.py @@ -0,0 +1,31 @@ +from aiogram.types import ( + InlineQueryResultArticle, InputTextMessageContent, +) + +from bot.modules.database import db +from bot.modules.error import Error + +from bot.common import console + + +async def get_error_search_results(error_id: str) -> (list[InlineQueryResultArticle] + | None): + error: Error = db.errors.get(error_id) + if error is None: + return [] + + console.print(f'{error_id} requested') + console.print(error.traceback) + console.print(f'-{error_id} requested-') + + return [( + InlineQueryResultArticle( + id=error_id, + title=f'Error {error_id}', + description=error.exception.short, + input_message_content=InputTextMessageContent( + message_text=error.exception.long, + parse_mode='HTML', + ), + ) + )]