File size: 1,254 Bytes
4566c49
 
 
 
 
 
 
 
 
 
 
a14f7f7
4566c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import html
import logging
import time

from aiogram import Bot
from aiogram.types import ErrorEvent

from app.config import get_settings

log = logging.getLogger(__name__)
_last_notified: dict[str, float] = {}
NOTICE = "⚠️ Something went wrong. Please try again."


async def on_error(event: ErrorEvent, bot: Bot) -> bool:
    exc = event.exception
    log.error("Unhandled error while processing an update", exc_info=exc)
    update = event.update
    try:  # never leave the user staring at a silent button
        if update.callback_query:
            await update.callback_query.answer(NOTICE[:190], show_alert=True)
        elif update.message:
            await update.message.answer(NOTICE)
    except Exception:
        pass

    key = f"{type(exc).__name__}:{str(exc)[:80]}"
    now = time.monotonic()
    if now - _last_notified.get(key, 0) > 300:  # at most one admin alert per distinct error / 5 min
        _last_notified[key] = now
        text = f"⚠️ <b>{html.escape(type(exc).__name__)}</b>\n<code>{html.escape(str(exc)[:1500])}</code>"
        for admin_id in get_settings().admin_id_set:
            try:
                await bot.send_message(admin_id, text)
            except Exception:
                pass
    return True