Add details to stdout error message.

Current glance command does not show the details of error message.
For example, the glance command shows HTTPBadRequest only if some
necessary parameter is not specified.

 $ glance image-create --file root-fs.img --name cirros-0.3.0-x86_64-uec
 Request returned failure status.
 HTTPBadRequest (HTTP 400)
 $

By only the above message, it is not easy that a user understand the reason
of an error. glance-api server returns the details of reason, but glance
command does not show it.

This patch adds details, which is gotten from glance-api server, to error
message. And a user will be able to understand the reason of a error like
the following:

 $ glance image-create --file root-fs.img --name cirros-0.3.0-x86_64-uec
 Request returned failure status.
 400 Bad Request
 Invalid disk format 'None' for image.
     (HTTP 400)
 $

Fixes bug 1094917

Change-Id: I49192c3ebbc8a70b63dcfcede9fd13f1688388cf
This commit is contained in:
Ken'ichi Ohmichi 2013-01-01 22:33:15 +09:00
parent c057fe47d6
commit 19d542ef5f
2 changed files with 8 additions and 4 deletions

View File

@ -177,7 +177,7 @@ class HTTPClient(object):
if 400 <= resp.status < 600:
LOG.error("Request returned failure status.")
raise exc.from_response(resp)
raise exc.from_response(resp, body_str)
elif resp.status in (301, 302, 305):
# Redirected. Reissue the request to the new location.
return self._http_request(resp['location'], method, **kwargs)

View File

@ -46,10 +46,10 @@ class HTTPException(ClientException):
code = 'N/A'
def __init__(self, details=None):
self.details = details
self.details = details or self.__class__.__name__
def __str__(self):
return "%s (HTTP %s)" % (self.__class__.__name__, self.code)
return "%s (HTTP %s)" % (self.details, self.code)
class HTTPMultipleChoices(HTTPException):
@ -150,9 +150,13 @@ for obj_name in dir(sys.modules[__name__]):
_code_map[obj.code] = obj
def from_response(response):
def from_response(response, body=None):
"""Return an instance of an HTTPException based on httplib response."""
cls = _code_map.get(response.status, HTTPException)
if body:
details = body.replace('\n\n', '\n')
return cls(details=details)
return cls()