LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.68k stars 1.38k forks source link

TypeError: ConnectionError() takes no keyword arguments #4437

Closed delobanov closed 2 weeks ago

delobanov commented 3 weeks ago

Code that causes the issue

I found problem in this patch of standard python_socks error.

https://github.com/LonamiWebs/Telethon/blob/75408483ad44a34553f40c02c375a2567c74831c/telethon/network/connection/connection.py#L115-L123

Expected behavior

Error python_socks._protocols.errors.ReplyError: Host unreachable should be handled properly.

Actual behavior

But it causes TypeError: ConnectionError() takes no keyword arguments.

Traceback

Telethon version

1.36.0

Python version

3.12

Operating system (including distribution name and version)

Ubuntu 22.04

Other details

No response

Checklist

Lonami commented 3 weeks ago

Seems you've investigated the issue a fair bit. Feel free to submit a pull request, as I don't personally use proxies.

delobanov commented 2 weeks ago

If I understand correctly, we need to handle ProxyError, ProxyConnectionError, ProxyTimeoutError errors in places where we already handle IOError in connection.py like in recv and send loops like this https://github.com/LonamiWebs/Telethon/blob/75408483ad44a34553f40c02c375a2567c74831c/telethon/network/connection/connection.py#L328-L355 and in mtprotosender.py in _try_connect, _connect, _reconnect, _send_loop and _recv_loop.

But I don't understand, why IOError handles when it seems to be already handled, example: in mtprotosender.py _reconnect: https://github.com/LonamiWebs/Telethon/blob/75408483ad44a34553f40c02c375a2567c74831c/telethon/network/mtprotosender.py#L376-L383 in mtprotosender.py _connect: https://github.com/LonamiWebs/Telethon/blob/75408483ad44a34553f40c02c375a2567c74831c/telethon/network/mtprotosender.py#L232-L255 and then in mtprotosender.py _try_connect: https://github.com/LonamiWebs/Telethon/blob/75408483ad44a34553f40c02c375a2567c74831c/telethon/network/mtprotosender.py#L281-L291

Looks like IOError already handled in _try_connect method and handling in _reconnect seems useless?

Maybe I am missing something? Will handling proxy errors in this places be enough?

Lonami commented 2 weeks ago

Too complicated. I'd make a subclass and use that:

class ConnectionErrorExtra(ConnectionError):
    def __init__(self, *args, **kwargs):
        super().__init__()

and then we can use this instead of the default ConnectionError.

delobanov commented 2 weeks ago

Please look for my fix (#4440 ). Is it okay to define Error class right in function? As for error_code argument I make definition like in original python_socks repository.

https://github.com/romis2012/python-socks/blob/98462c52a5ea174c055c4393ffe64f1ac81f5842/python_socks/_errors.py#L1-L4

Lonami commented 2 weeks ago

Is it okay to define Error class right in function?

That's fine. It means people won't import it (or shouldn't), but it's okay because it inherits ConnectionError.