Unhandled AttributeArrors when Content-Type header not set in response

Bug #1870264 reported by Simon Merrick
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-magnumclient
Fix Released
Undecided
Unassigned

Bug Description

If the HTTP response from magnum API does not set the Content-Type header then magnum client raises unhandled AttributeError.

This makes it difficult to handle errors gracefully in horizon (magnum UI) which will either throw a 500 if the error is not caught - or is otherwise difficult to provided a more meaningfull erorr message for the client.

AttributeError: HTTPResponse instance has no attribute 'headers'

2020-04-01 04:18:27,644 28809 ERROR openstack_dashboard.api.rest.utils error invoking apiclient
Traceback (most recent call last):
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/openstack_dashboard/api/rest/utils.py", line 127, in _wrapped
    data = function(self, request, *args, **kw)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnum_ui/api/rest/magnum.py", line 307, in post
    new_cluster = magnum.cluster_create(request, **request.DATA)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnum_ui/api/magnum.py", line 165, in cluster_create
    return magnumclient(request).clusters.create(**args)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnumclient/v1/baseunit.py", line 102, in create
    return self._create(self._path(), new)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnumclient/common/base.py", line 49, in _create
    resp, body = self.api.json_request('POST', url, body=body)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 233, in json_request
    resp, body_iter = self._http_request(url, method, **kwargs)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 213, in _http_request
    error_json = _extract_error_json(body_str, resp)
  File "/opt/cat/openstack/horizon/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 63, in _extract_error_json
    content_type = resp.headers.get("Content-Type", "")
AttributeError: HTTPResponse instance has no attribute 'headers'
2020-04-01 04:18:41,282 28814 WARNING django.request Not Found: /api/container_infra/quotas/9dbaf423e94644a699c2a6202a811ce4/Cluster

Revision history for this message
Simon Merrick (iokiwi) wrote :

I'm happy to submit a patch to fix this, but I raised it here to track it none the less

Revision history for this message
Tobias Urdin (tobias-urdin) wrote :

Same here.

  File "/usr/share/openstack-dashboard/openstack_dashboard/api/rest/utils.py", line 128, in _wrapped
    data = function(self, request, *args, **kw)
  File "/usr/lib/python2.7/site-packages/magnum_ui/api/rest/magnum.py", line 53, in patch
    request, template_id, **params)
  File "/usr/lib/python2.7/site-packages/magnum_ui/api/magnum.py", line 146, in cluster_template_update
    return magnumclient(request).cluster_templates.update(id, patch)
  File "/usr/lib/python2.7/site-packages/magnumclient/v1/basemodels.py", line 118, in update
    return self._update(self._path(id), patch)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/base.py", line 131, in _update
    resp, resp_body = self.api.json_request(method, url, body=body)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 233, in json_request
    resp, body_iter = self._http_request(url, method, **kwargs)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 213, in _http_request
    error_json = _extract_error_json(body_str, resp)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 63, in _extract_error_json
    content_type = resp.headers.get("Content-Type", "")
AttributeError: HTTPResponse instance has no attribute 'headers'

Revision history for this message
Tobias Urdin (tobias-urdin) wrote :

Issue is that it assumes a python-requests response class but it might be a HTTPResponse. We should assume using the body unless it's a requests response.

    return self._update(self._path(id), patch)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/base.py", line 131, in _update
    resp, resp_body = self.api.json_request(method, url, body=body)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 233, in json_request
    resp, body_iter = self._http_request(url, method, **kwargs)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 213, in _http_request
    error_json = _extract_error_json(body_str, resp)
  File "/usr/lib/python2.7/site-packages/magnumclient/common/httpclient.py", line 66, in _extract_error_json
    body_json = resp.json()
AttributeError: HTTPResponse instance has no attribute 'json'

Revision history for this message
Simon Merrick (iokiwi) wrote :

Oh right, my bad I totally missed the fact that it's an entirely different response class.

I am not sure I understand what you mean by:
>We should assume using the body unless it's a requests response.

I wonder what the reason behind bundling /common/httpclient.py was instead of using python-requests and I wonder how the magnum team would feel about standardising on requests.

At the very least magnum client should standardize on a single http client.

Revision history for this message
Tobias Urdin (tobias-urdin) wrote :
Changed in python-magnumclient:
status: New → Fix Committed
Utian Ayuba (utianayuba)
Changed in python-magnumclient:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.