Fix youtube downloading wrong track, saving exceptions, attempt to fix deezer
This commit is contained in:
1
bot/modules/error/__init__.py
Normal file
1
bot/modules/error/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .handler import on_error
|
||||
53
bot/modules/error/handler.py
Normal file
53
bot/modules/error/handler.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from bot.common import console
|
||||
from aiogram.types.error_event import ErrorEvent
|
||||
from aiogram import Bot
|
||||
|
||||
from rich.traceback import Traceback
|
||||
|
||||
from bot.modules.database import db
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Error:
|
||||
traceback: Traceback
|
||||
inline_message_id: str | None = None
|
||||
|
||||
|
||||
async def on_error(event: ErrorEvent, bot: Bot):
|
||||
import os
|
||||
import base64
|
||||
|
||||
error_id = base64.urlsafe_b64encode(os.urandom(6)).decode()
|
||||
|
||||
traceback = Traceback.from_exception(
|
||||
type(event.exception),
|
||||
event.exception,
|
||||
event.exception.__traceback__,
|
||||
show_locals=True,
|
||||
max_frames=1,
|
||||
)
|
||||
|
||||
if event.update.chosen_inline_result:
|
||||
db.errors[error_id] = Error(
|
||||
traceback=traceback,
|
||||
inline_message_id=event.update.chosen_inline_result.inline_message_id,
|
||||
)
|
||||
|
||||
await bot.edit_message_caption(
|
||||
inline_message_id=event.update.chosen_inline_result.inline_message_id,
|
||||
caption=f'💔 <b>ERROR</b> occurred. Use this code to get more information: '
|
||||
f'<code>{error_id}</code>',
|
||||
parse_mode='HTML',
|
||||
)
|
||||
|
||||
else:
|
||||
db.errors[error_id] = Error(
|
||||
traceback=traceback,
|
||||
)
|
||||
|
||||
console.print(f'[red]{error_id} occurred[/]')
|
||||
console.print(event)
|
||||
console.print(traceback)
|
||||
console.print(f'-{error_id}-')
|
||||
50
bot/modules/error/pretty.py
Normal file
50
bot/modules/error/pretty.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import os
|
||||
import traceback
|
||||
import contextlib
|
||||
import re
|
||||
|
||||
|
||||
class PrettyException:
|
||||
def __init__(self, e: Exception):
|
||||
self.pretty_exception = f"""
|
||||
❌ Error! Report it to admins:
|
||||
🐊 <code>{e.__traceback__.tb_frame.f_code.co_filename.replace(os.getcwd(), "")}\r
|
||||
</code>:{e.__traceback__.tb_frame.f_lineno}
|
||||
😍 {e.__class__.__name__}
|
||||
👉 {"".join(traceback.format_exception_only(e)).strip()}
|
||||
|
||||
⬇️ Trace:
|
||||
{self.get_full_stack()}
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def get_full_stack():
|
||||
full_stack = traceback.format_exc().replace(
|
||||
"Traceback (most recent call last):\n", ""
|
||||
)
|
||||
|
||||
line_regex = r' File "(.*?)", line ([0-9]+), in (.+)'
|
||||
|
||||
def format_line(line: str) -> str:
|
||||
filename_, lineno_, name_ = re.search(line_regex, line).groups()
|
||||
with contextlib.suppress(Exception):
|
||||
filename_ = os.path.basename(filename_)
|
||||
|
||||
return (
|
||||
f"🤯 <code>{filename_}:{lineno_}</code> (<b>in</b>"
|
||||
f" <code>{name_}</code> call)"
|
||||
)
|
||||
|
||||
full_stack = "\n".join(
|
||||
[
|
||||
format_line(line)
|
||||
if re.search(line_regex, line)
|
||||
else f"<code>{line}</code>"
|
||||
for line in full_stack.splitlines()
|
||||
]
|
||||
)
|
||||
|
||||
return full_stack
|
||||
|
||||
def __str__(self):
|
||||
return self.pretty_exception
|
||||
Reference in New Issue
Block a user