0

I'm trying to add a try-catch to my script that is supposed to notify my when my script is done executing using telegram_send(). So I ran the script with the internet connection off to see what error is raised by the function so I could catch it and add a small print() message to inform the user that the internet was out. What I got, however is this:

Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connection.py", line 140, in _new_conn
    conn = connection.create_connection(
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "C:\Python\Python38\lib\socket.py", line 914, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 614, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 360, in _make_request
    self._validate_conn(conn)
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 857, in _validate_conn
    super(HTTPSConnectionPool, self)._validate_conn(conn)
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 289, in _validate_conn
    conn.connect()
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connection.py", line 284, in connect
    conn = self._new_conn()
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connection.py", line 149, in _new_conn
    raise NewConnectionError(
telegram.vendor.ptb_urllib3.urllib3.exceptions.NewConnectionError: <telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000014144AA8100>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\telegram\utils\request.py", line 259, in _request_wrapper
    resp = self._con_pool.request(*args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 68, in request
    return self.request_encode_body(method, url, fields=fields,
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\poolmanager.py", line 244, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 691, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 691, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 691, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 665, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "C:\Python\Python38\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\retry.py", line 376, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
telegram.vendor.ptb_urllib3.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /TOKEN/sendMessage (Caused by NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000014144AA8100>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\chris\Documents\GitHub\QSAR-Versuch\ROC.py", line 212, in <module>
    telegram_send.send(messages=['OI',time_string])
  File "C:\Python\Python38\lib\site-packages\telegram_send\telegram_send.py", line 246, in send
    message_ids += [send_message(m, parse_mode)["message_id"]]
  File "C:\Python\Python38\lib\site-packages\telegram_send\telegram_send.py", line 228, in send_message
    return bot.send_message(
  File "C:\Python\Python38\lib\site-packages\telegram\bot.py", line 133, in decorator
    result = func(*args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\telegram\bot.py", line 525, in send_message
    return self._message(  # type: ignore[return-value]
  File "C:\Python\Python38\lib\site-packages\telegram\bot.py", line 339, in _message
    result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
  File "C:\Python\Python38\lib\site-packages\telegram\bot.py", line 298, in _post
    return self.request.post(
  File "C:\Python\Python38\lib\site-packages\telegram\utils\request.py", line 361, in post
    result = self._request_wrapper(
  File "C:\Python\Python38\lib\site-packages\telegram\utils\request.py", line 265, in _request_wrapper
    raise NetworkError(f'urllib3 HTTPError {error}') from error
telegram.error.NetworkError: urllib3 HTTPError HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot5484246240:TOKEN/sendMessage (Caused by NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x0000014144AA8100>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

Now call me crazy, but I find that traceback to be slightly confusing. I am used to having a simple builtin error raised by python so I'm at a loss what to do in this case, since it seems like there is a custom error type that I'd need to import from urlib or something and then make the try-catch with that. So, how do I fix this? Is there a hacky way to do it?

Not really useful, but here's a script example:

import telegram_send

try:
    telegram_send.send(messages=['OI!'])
except SomeErrorIfInternetIsOut:
    print('There was no internet connection.')

EDIT: I tried to import socket.gaierror and checking for that, but no chage. The traceback stays exactly the same.

J.Doe
  • 224
  • 1
  • 4
  • 19

1 Answers1

1

If you try to catch the error without specifying the type, you can see the error type is telegram.error.NetworkError (which is the last one in your stack trace).

Then, if you want to write an except statement specific to this error, you can first import telegram.error as tg_error in your code and change your except statement to except tg_error.NetworkError as e:).

My answer comes a bit late so I hope you found a solution or a workaround before, otherwise I hope this will be usefull to others.

Moustrash
  • 28
  • 5