首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当没有互联网连接时,如何捕捉telegram_send中的错误?

当没有互联网连接时,如何捕捉telegram_send中的错误?
EN

Stack Overflow用户
提问于 2022-10-13 12:24:49
回答 1查看 63关注 0票数 0

我试图在我的脚本中添加一个try-catch,它应该在我的脚本使用telegram_send()执行完毕时通知我的脚本。因此,我运行脚本与互联网连接,以查看什么错误是由该函数,以便我可以抓住它,并添加一个小的print()消息,以通知用户互联网已经关闭。然而,我得到的是:

代码语言:javascript
复制
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'))

现在说我疯了,但我觉得这种回溯有点让人困惑。我已经习惯了python引发的一个简单的内置错误,所以我不知道在这种情况下应该做什么,因为似乎有一种自定义错误类型需要从urlib或其他地方导入,然后用它进行尝试-捕捉。那我该怎么解决这个问题?有什么麻烦事吗?

并不是很有用,但下面是一个脚本示例:

代码语言:javascript
复制
import telegram_send

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

编辑:我试着导入socket.gaierror并检查它,但是没有问题。追踪器保持原样。

EN

Stack Overflow用户

回答已采纳

发布于 2022-11-17 11:01:03

如果尝试catch the error without specifying the type,可以看到错误类型是telegram.error.NetworkError (这是堆栈跟踪中的最后一个错误类型)。

然后,如果您想要编写一个特定于此错误的and语句,您可以首先在代码中import telegram.error as tg_error并将您的and语句更改为except tg_error.NetworkError as e:)。

我的答案来得有点晚,所以我希望你找到一个解决方案或解决办法之前,否则我希望这将是有益于其他人。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74055738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档