error_handler.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import logging
  2. from aiogram.utils.exceptions import (TelegramAPIError,
  3. MessageNotModified,
  4. CantParseEntities)
  5. from loader import dp
  6. @dp.errors_handler()
  7. async def errors_handler(update, exception):
  8. """
  9. Exceptions handler. Catches all exceptions within task factory tasks.
  10. :param dispatcher:
  11. :param update:
  12. :param exception:
  13. :return: stdout logging
  14. """
  15. if isinstance(exception, MessageNotModified):
  16. logging.exception('Message is not modified')
  17. # do something here?
  18. return True
  19. if isinstance(exception, CantParseEntities):
  20. # or here
  21. logging.exception(f'CantParseEntities: {exception} \nUpdate: {update}')
  22. return True
  23. # MUST BE THE LAST CONDITION (ЭТО УСЛОВИЕ ВСЕГДА ДОЛЖНО БЫТЬ В КОНЦЕ)
  24. if isinstance(exception, TelegramAPIError):
  25. logging.exception(f'TelegramAPIError: {exception} \nUpdate: {update}')
  26. return True
  27. # At least you have tried.
  28. logging.exception(f'Update: {update} \n{exception}')