Print unhandled errors to stderr if logging is not configured

This should mitigate "the code doesn't work but there are no errors"
situations. Users not wanting this behaviour can configure logging
with a high-enough level that won't print anything, or set a filter.
This commit is contained in:
Lonami Exo 2021-01-30 13:47:28 +01:00
parent 4cc9645d76
commit b88ec4b814

View File

@ -2,8 +2,11 @@ import asyncio
import inspect import inspect
import itertools import itertools
import random import random
import sys
import time import time
import traceback
import typing import typing
import logging
from .. import events, utils, errors from .. import events, utils, errors
from ..events.common import EventBuilder, EventCommon from ..events.common import EventBuilder, EventCommon
@ -281,6 +284,16 @@ class UpdateMethods:
# region Private methods # region Private methods
def _log_exc(self: 'TelegramClient', msg, *args):
"""
Log an exception, using `stderr` if `logging` is not configured.
"""
if logging.root.hasHandlers():
self._log[__name__].exception(msg, *args)
else:
print('[ERROR/telethon]:', msg % args, file=sys.stderr)
traceback.print_exc()
# It is important to not make _handle_update async because we rely on # It is important to not make _handle_update async because we rely on
# the order that the updates arrive in to update the pts and date to # the order that the updates arrive in to update the pts and date to
# be always-increasing. There is also no need to make this async. # be always-increasing. There is also no need to make this async.
@ -464,8 +477,7 @@ class UpdateMethods:
except Exception as e: except Exception as e:
if not isinstance(e, asyncio.CancelledError) or self.is_connected(): if not isinstance(e, asyncio.CancelledError) or self.is_connected():
name = getattr(callback, '__name__', repr(callback)) name = getattr(callback, '__name__', repr(callback))
self._log[__name__].exception('Unhandled exception on %s', self._log_exc('Unhandled exception on %s', name)
name)
async def _dispatch_event(self: 'TelegramClient', event): async def _dispatch_event(self: 'TelegramClient', event):
""" """
@ -506,8 +518,7 @@ class UpdateMethods:
except Exception as e: except Exception as e:
if not isinstance(e, asyncio.CancelledError) or self.is_connected(): if not isinstance(e, asyncio.CancelledError) or self.is_connected():
name = getattr(callback, '__name__', repr(callback)) name = getattr(callback, '__name__', repr(callback))
self._log[__name__].exception('Unhandled exception on %s', self._log_exc('Unhandled exception on %s', name)
name)
async def _get_difference(self: 'TelegramClient', update, channel_id, pts_date): async def _get_difference(self: 'TelegramClient', update, channel_id, pts_date):
""" """
@ -610,8 +621,7 @@ class UpdateMethods:
self._log[__name__].warning('Failed to get missed updates after ' self._log[__name__].warning('Failed to get missed updates after '
'reconnect: %r', e) 'reconnect: %r', e)
except Exception: except Exception:
self._log[__name__].exception('Unhandled exception while getting ' self._log_exc('Unhandled exception while getting update difference after reconnect')
'update difference after reconnect')
# endregion # endregion