处理嵌套的ConnectionError异常

时间:2018-08-24 01:54:39

标签: python python-3.x

我正在运行与Rest API通信的脚本。有时,它返回一个异常,对于另外两个异常,该异常也返回During handling of the above exception, another exception occurred:。我试图使用try / except块来处理它,但是尽管导入了所有三个异常并在块中为它们编码,脚本仍然失败。

三个例外是http.client.RemoteDisconnectedurllib3.exceptions.ProtocolErrorrequests.exceptions.ConnectionError

所以我的导入代码是:

from requests.exceptions import ConnectionError
from http.client import RemoteDisconnected
from urllib3.exceptions import ProtocolError

我的IDE将异常识别为已导入,因此可能没问题。我的除外代码是:

try
    [the thing]
except (InternalError, ConnectionError, RemoteDisconnected, ProtocolError) as e:
    self.lg.log(str(e) + ' ' + str(e.__traceback__) + " Some sort of connection or 
            internal error occured")

我在同一块(InternalError)上还有另一个异常,那个异常似乎可以处理。

回溯确实很长,但是我不确定哪个部分最重要,所以这就是全部

Traceback (most recent call last):
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\Rominus\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\Rominus\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Rominus\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\requests\adapters.py", line 445, in send
    timeout=timeout
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\util\retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\Rominus\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Users\Rominus\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Rominus\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Rominus/PycharmProjects/Binance/Skitter_trade_test.py", line 55, in <module>
    bintime = int(client.time()['serverTime'])
  File "C:\Users\Rominus\PycharmProjects\Binance\api.py", line 98, in time
    return self._get('v1/time')
  File "C:\Users\Rominus\PycharmProjects\Binance\api.py", line 62, in _get
    return self._request(endpoint, param, "get")
  File "C:\Users\Rominus\PycharmProjects\Binance\api.py", line 40, in _request
    r = getattr(self.session, http_method)(BinanceAPI.API_URL + endpoint, params=params)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\requests\sessions.py", line 525, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Rominus\PycharmProjects\Binance\venv\lib\site-packages\requests\adapters.py", line 495, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

0 个答案:

没有答案