Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: construct InfluxDBError without http response #375

Merged
merged 2 commits into from
Dec 6, 2021

Conversation

wasted925
Copy link
Contributor

@wasted925 wasted925 commented Dec 4, 2021

Proposed Changes

This fixes an AttributeError while handling another error raised from a failed POST request. In detail, when a POST request fails during the connection establishment there is no http response, so when an InfluxDBError instance is constructed the response field is None . This leads to an AttributeError later when the response.data is accessed. The fix is a trivial None check in the InfluxDBError constructor so that a None response argument properly works.

I was able to reproduce it due to the expired ssl certificate error with the following traceback (Python 3.10/Windows server 2016):

    httplib_response = self._make_request(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connection.py", line 416, in connect
    self.sock = ssl_wrap_socket(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\util\ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\util\ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Python310\lib\ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Python310\lib\ssl.py", line 1070, in _create
    self.do_handshake()
  File "C:\Python310\lib\ssl.py", line 1341, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\rest.py", line 217, in request
    r = self.pool_manager.request(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\request.py", line 78, in request
    return self.request_encode_body(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\request.py", line 170, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\poolmanager.py", line 375, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\util\retry.py", line 507, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\connection.py", line 416, in connect
    self.sock = ssl_wrap_socket(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\util\ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
  File "C:\dev\python_env\prod\lib\site-packages\urllib3\util\ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Python310\lib\ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Python310\lib\ssl.py", line 1070, in _create
    self.do_handshake()
  File "C:\Python310\lib\ssl.py", line 1341, in do_handshake
    self._sslobj.do_handshake()
urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\dev\coinbot\src\coinbot\rolling_iv\main.py", line 16, in amain
    await script_func(cancel_token)
  File "C:\dev\coinbot\src\coinbot\rolling_iv\rolling_iv.py", line 299, in periodic_compute
    persist(pivots_tbl, next_time, coin)
  File "C:\dev\python_env\prod\lib\site-packages\tenacity\__init__.py", line 324, in wrapped_f
    return self(f, *args, **kw)
  File "C:\dev\python_env\prod\lib\site-packages\tenacity\__init__.py", line 404, in __call__
    do = self.iter(retry_state=retry_state)
  File "C:\dev\python_env\prod\lib\site-packages\tenacity\__init__.py", line 360, in iter
    raise retry_exc.reraise()
  File "C:\dev\python_env\prod\lib\site-packages\tenacity\__init__.py", line 193, in reraise
    raise self.last_attempt.result()
  File "C:\Python310\lib\concurrent\futures\_base.py", line 438, in result
    return self.__get_result()
  File "C:\Python310\lib\concurrent\futures\_base.py", line 390, in __get_result
    raise self._exception
  File "C:\dev\python_env\prod\lib\site-packages\tenacity\__init__.py", line 407, in __call__
    result = fn(*args, **kwargs)
  File "C:\dev\coinbot\src\coinbot\rolling_iv\rolling_iv.py", line 266, in persist
    write_api.write(_influx_bucket, _influx_org, points)
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\client\write_api.py", line 371, in write
    results = list(map(write_payload, payloads.items()))
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\client\write_api.py", line 369, in write_payload
    return self._post_write(_async_req, bucket, org, final_string, payload[0])
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\client\write_api.py", line 517, in _post_write
    return self._write_service.post_write(org=org, bucket=bucket, body=body, precision=precision,
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\service\write_service.py", line 62, in post_write
    (data) = self.post_write_with_http_info(org, bucket, body, **kwargs)  # noqa: E501
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\service\write_service.py", line 166, in post_write_with_http_info
    return self.api_client.call_api(
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\api_client.py", line 341, in call_api
    return self.__call_api(resource_path, method,
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\api_client.py", line 171, in __call_api
    response_data = self.request(
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\api_client.py", line 386, in request
    return self.rest_client.POST(url,
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\rest.py", line 304, in POST
    return self.request("POST", url,
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\rest.py", line 240, in request
    raise ApiException(status=0, reason=msg)
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\rest.py", line 360, in __init__
    super().__init__(response=http_resp)
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\client\exceptions.py", line 16, in __init__
    self.message = self._get_message(response)
  File "C:\dev\python_env\prod\lib\site-packages\influxdb_client\client\exceptions.py", line 22, in _get_message
    if response.data:
AttributeError: 'NoneType' object has no attribute 'data'

Checklist

  • CHANGELOG.md updated
  • Rebased/mergeable
  • A test has been added if appropriate
  • pytest tests completes successfully
  • Commit messages are in semantic format
  • Sign CLA (if not already signed)

@codecov-commenter
Copy link

codecov-commenter commented Dec 4, 2021

Codecov Report

Merging #375 (e035c75) into master (cb23528) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #375      +/-   ##
==========================================
+ Coverage   92.89%   92.90%   +0.01%     
==========================================
  Files          28       28              
  Lines        2392     2396       +4     
==========================================
+ Hits         2222     2226       +4     
  Misses        170      170              
Impacted Files Coverage Δ
influxdb_client/client/exceptions.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cb23528...e035c75. Read the comment docs.

Copy link
Contributor

@bednar bednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wasted925 thanks for your PR 👍

LGTM

@bednar bednar merged commit 7e09dde into influxdata:master Dec 6, 2021
@bednar bednar added this to the 1.25.0 milestone Dec 6, 2021
@bednar bednar added the bug Something isn't working label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants