python client doesn't surface errors

Bug #1368462 reported by Tim Hinrichs
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
congress
Fix Released
High
Zhenzan Zhou

Bug Description

Errors returned by the APi are not returned by the python client. In fact, some API errors cause uncaught exceptions in the Python client. For example, creating an unsafe rule causes an uncaught exception.

Tim Hinrichs (thinrichs)
Changed in congress:
importance: Undecided → Medium
Tim Hinrichs (thinrichs)
Changed in congress:
importance: Medium → High
Revision history for this message
Aaron Rosen (arosen) wrote :

Mind including an example of how you provide this in the bug report?

Revision history for this message
Tim Hinrichs (thinrichs) wrote :

Here's what we see from the CLI...

nicira@Ubuntu1204Server:/opt/stack/python-congressclient$ openstack congress policy rule create classification "p(x) :- q(x"
ERROR: openstack Bad Request (HTTP 400) (Request-ID: req-cdfdd7e7-19e5-4da6-8d40-4391ca235c21)

Here's what we get from curl. Notice that we get an informative error message: "Syntax error for rule::Parse failure..."

nicira@Ubuntu1204Server:/opt/stack/python-congressclient$ curl -i -X POST http://192.168.1.60:1789/v1/policies/classification/rules -H "User-Agent: python-congressclient" -H "X-Auth-Token:d6eb33a5634a5af84da85e8a02ecac10bc9a34ac" -d '{"rule": "p(x) :- q(x"}'
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=UTF-8
Content-Length: 149
X-Openstack-Request-Id: req-7c50c528-e72b-486e-9cf3-8023a10e711a
Date: Fri, 31 Oct 2014 19:50:55 GMT

{"error_data": null, "error_code": 1002, "description": "Syntax error for rule::Parse failure.\nline:1,col:10 no viable alternative at input u'x'"}

Samta Rangare (srangare)
Changed in congress:
assignee: nobody → Samta Rangare (srangare)
Tim Hinrichs (thinrichs)
Changed in congress:
assignee: Samta Rangare (srangare) → nobody
Changed in congress:
assignee: nobody → Zhenzan Zhou (zhenzan-zhou)
status: New → In Progress
Revision history for this message
Zhenzan Zhou (zhenzan-zhou) wrote :

We do get a HTTPClientError exception, but we lost the details because the response json from Congress has a different schema(?):

python-keystoneclient/keystoneclient/openstack/common/apiclient/exceptions.py:

    if content_type.startswith("application/json"):
        try:
            body = response.json()
        except ValueError:
            pass
        else:
            if isinstance(body, dict) and isinstance(body.get("error"), dict):
                error = body["error"]
                kwargs["message"] = error.get("message")
                kwargs["details"] = error.get("details")

JSON we get from Congress server:

{"error_data": null, "error_code": 1002, "description": "Syntax error for rule::Parse failure.\nline:1,col:10 no viable alternative at input u'x'"}

So we lost the details while doing exception handling. To get the detail, at first we could change congress/api/webservice.py:error_response() raw_body. E.g.

    raw_body = { 'error': {
        'error_code': error_code,
        #'description': description,
        'details': description,
        #'error_data': data
        'message': data
        }
    }

Now we can push it to HTTPClientError, but we still cannot have it at the final stage yet:

(Pdb) p kwargs
{'url': u'http://10.239.47.118:1789/v1/policies/classification/rules', 'method': 'POST', 'details': u"Syntax error for rule::Parse failure.\nline:1,col:10 no viable alternative at input u'x'", 'http_status': 400, 'request_id': 'req-70892898-62fc-4ff0-96ef-bb78c4c80b10', 'message': None, 'response': <Response [400]>}
...
BadRequest: Bad Request (HTTP 400) (Request-ID: req-70892898-62fc-4ff0-96ef-bb78c4c80b10)
--Return--
> /usr/lib/python2.7/logging/__init__.py(1178)error()->None
-> self._log(ERROR, msg, args, **kwargs)
(Pdb) p ERROR
40
(Pdb) p msg
BadRequest(u'Bad Request (HTTP 400) (Request-ID: req-70892898-62fc-4ff0-96ef-bb78c4c80b10)',)
(Pdb) p args
()
(Pdb) p kwargs
{'exc_info': 1}

Revision history for this message
Zhenzan Zhou (zhenzan-zhou) wrote :

As congressclient commands are implemented as openstackclient sub commands, the exception is finally
handled by cliff/app.py:run_subcommand() which just calls log.error() with BadRequest exception. The details
are ignored except the "message" field which is used to construct the exception. So I think all openstackclient
subcommands suffers.

In my opinion, the simple solution is to pass down congress detail in "message" field, i.e.

    raw_body = { 'error': {
        'error_code': error_code,
        'details': data,
        'message': description
        }
    }

Here is an example output:

zhenzan@zhenzan-openstack:~/workspace/congress$ /usr/local/bin/openstack congress policy rule create \
classification "p(x) :- q(x"
ERROR: openstack Syntax error for rule::Parse failure.
line:1,col:10 no viable alternative at input u'x' (HTTP 400) (Request-ID: req-c28c17a9-fa1b-49e8-99e9-b316be170429)

The detail push back is:
Syntax error for rule::Parse failure.
line:1,col:10 no viable alternative at input u'x'

It's not pretty but has the detail and works for all congressclient commands.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to congress (master)

Fix proposed to branch: master
Review: https://review.openstack.org/147385

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to congress (master)

Reviewed: https://review.openstack.org/147385
Committed: https://git.openstack.org/cgit/stackforge/congress/commit/?id=5c128f3740200ced5224a29df88f0adca4cb0ca1
Submitter: Jenkins
Branch: master

commit 5c128f3740200ced5224a29df88f0adca4cb0ca1
Author: Zhenzan Zhou <email address hidden>
Date: Thu Jan 15 13:35:59 2015 +0800

    Use error.message in error response

    Openstack apiclient exception requires an error json object and
    its 'message' field will be used to construct output error message.
    The output is not pretty but has the detail and works for all
    congressclient commands.

    Change-Id: I881f2579c147fb25e7669282a6e5f9be889e3baf
    Closes-Bug: #1368462

Changed in congress:
status: In Progress → Fix Committed
Tim Hinrichs (thinrichs)
Changed in congress:
milestone: none → kilo-3
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.