Change TypeError with ValueError, don't call .get_dialogs()

This closes #735 and #736 since now it can be properly handled
from user code, and behave more correctly depending on the situation.

Also the errors provide more information on how to get around it.
This commit is contained in:
Lonami Exo 2018-04-04 13:35:51 +02:00
parent 55b5fca6fd
commit 591e34b491

View File

@ -38,7 +38,8 @@ from .errors import (
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError, RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError, PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError,
SessionPasswordNeededError, FileMigrateError, PhoneNumberUnoccupiedError, SessionPasswordNeededError, FileMigrateError, PhoneNumberUnoccupiedError,
PhoneNumberOccupiedError, EmailUnconfirmedError, PasswordEmptyError PhoneNumberOccupiedError, EmailUnconfirmedError, PasswordEmptyError,
UsernameNotOccupiedError
) )
from .network import ConnectionMode from .network import ConnectionMode
from .tl.custom import Draft, Dialog from .tl.custom import Draft, Dialog
@ -192,9 +193,6 @@ class TelegramClient(TelegramBareClient):
# Sometimes we need to know who we are, cache the self peer # Sometimes we need to know who we are, cache the self peer
self._self_input_peer = None self._self_input_peer = None
# Don't call .get_dialogs() every time a .get_entity() fails
self._called_get_dialogs = False
# endregion # endregion
# region Telegram requests functions # region Telegram requests functions
@ -2391,15 +2389,21 @@ class TelegramClient(TelegramBareClient):
invite = self(CheckChatInviteRequest(username)) invite = self(CheckChatInviteRequest(username))
if isinstance(invite, ChatInvite): if isinstance(invite, ChatInvite):
raise ValueError( raise ValueError(
'Cannot get entity from a channel ' 'Cannot get entity from a channel (or group) '
'(or group) that you are not part of' 'that you are not part of. Join the group and retry'
) )
elif isinstance(invite, ChatInviteAlready): elif isinstance(invite, ChatInviteAlready):
return invite.chat return invite.chat
elif username: elif username:
if username in ('me', 'self'): if username in ('me', 'self'):
return self.get_me() return self.get_me()
try:
result = self(ResolveUsernameRequest(username)) result = self(ResolveUsernameRequest(username))
except UsernameNotOccupiedError as e:
raise ValueError('No user has "{}" as username'
.format(username)) from e
for entity in itertools.chain(result.users, result.chats): for entity in itertools.chain(result.users, result.chats):
if getattr(entity, 'username', None) or ''\ if getattr(entity, 'username', None) or ''\
.lower() == username: .lower() == username:
@ -2410,8 +2414,8 @@ class TelegramClient(TelegramBareClient):
except ValueError: except ValueError:
pass pass
raise TypeError( raise ValueError(
'Cannot turn "{}" into any entity (user or chat)'.format(string) 'Cannot find any entity corresponding to "{}"'.format(string)
) )
def get_input_entity(self, peer): def get_input_entity(self, peer):
@ -2460,25 +2464,12 @@ class TelegramClient(TelegramBareClient):
'Cannot turn "{}" into an input entity.'.format(original_peer) 'Cannot turn "{}" into an input entity.'.format(original_peer)
) )
# Add the mark to the peers if the user passed a Peer (not an int), raise ValueError(
# or said ID is negative. If it's negative it's been marked already.
# Look in the dialogs with the hope to find it.
if not self._called_get_dialogs:
self._called_get_dialogs = True
mark = not isinstance(peer, int) or peer < 0
target_id = utils.get_peer_id(peer)
if mark:
for dialog in self.get_dialogs(100):
if utils.get_peer_id(dialog.entity) == target_id:
return utils.get_input_peer(dialog.entity)
else:
for dialog in self.get_dialogs(100):
if dialog.entity.id == target_id:
return utils.get_input_peer(dialog.entity)
raise TypeError(
'Could not find the input entity corresponding to "{}". ' 'Could not find the input entity corresponding to "{}". '
'Make sure you have encountered this peer before.'.format(peer) 'Make sure you have encountered this user/chat/channel before. '
'If the peer is in your dialogs call client.get_dialogs().'
'If the peer belongs to a chat call client.get_participants().'
.format(peer)
) )
def edit_2fa(self, current_password=None, new_password=None, hint='', def edit_2fa(self, current_password=None, new_password=None, hint='',