Skip to content

Instantly share code, notes, and snippets.

@Forevka
Created April 19, 2021 08:25
Show Gist options
  • Save Forevka/c4d00a9f716b5c79123786383b06f4c0 to your computer and use it in GitHub Desktop.
Save Forevka/c4d00a9f716b5c79123786383b06f4c0 to your computer and use it in GitHub Desktop.
import asyncio
import logging
import os
from aiogram import Bot, Dispatcher, executor, types
from aiogram.utils.exceptions import MessageNotModified
from aiogram.dispatcher.filters.filters import BoundFilter
API_TOKEN = os.environ.get('TOKEN', '')
logging.basicConfig(level=logging.INFO)
class RethrowExceptionFilter(BoundFilter):
key = 'rethrow'
required = True
default = True
def __init__(self, rethrow: bool,):
self.rethrow = rethrow
async def check(self, obj, error):
if (hasattr(error, 'aiogram_is_handled')):
return False
setattr(error, 'aiogram_is_handled', True)
return True
async def send_welcome(message: types.Message):
constant_text = "Error going brrrrrrrrrrrrrr"
msg = await message.answer(constant_text)
await asyncio.sleep(1)
await msg.edit_text(constant_text)
async def throw_error(message: types.Message):
raise ValueError('test')
async def catch_not_modified(update: types.Update, error):
print('not modified', update.update_id)
return True
async def catch_all(update: types.Update, error):
print('all other', update.update_id)
return True
if __name__ == '__main__':
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
dp.filters_factory.bind(RethrowExceptionFilter, event_handlers=[
dp.errors_handlers,
])
dp.register_message_handler(throw_error, commands=['t'])
dp.register_message_handler(send_welcome)
dp.register_errors_handler(catch_not_modified, exception=MessageNotModified)
dp.register_errors_handler(catch_all, exception=Exception)
executor.start_polling(dp, skip_updates=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment