Add optional error_message to adapter.request
When we make REST cals, sometimes we want to provide a more helpful error string. Add an optional argument to the calls to allow this. Change-Id: I797e34cda9f27c18d66a42fd5860fe572fa55ada
This commit is contained in:
parent
8a4f99720f
commit
e81bec084a
|
@ -111,8 +111,8 @@ class ShadeAdapter(adapter.Adapter):
|
|||
self.request_log.debug(tmpl.format(**kwargs))
|
||||
return response
|
||||
|
||||
def _munch_response(self, response, result_key=None):
|
||||
exc.raise_from_response(response)
|
||||
def _munch_response(self, response, result_key=None, error_message=None):
|
||||
exc.raise_from_response(response, error_message=error_message)
|
||||
|
||||
if not response.content:
|
||||
# This doens't have any content
|
||||
|
@ -154,7 +154,9 @@ class ShadeAdapter(adapter.Adapter):
|
|||
return meta.obj_to_dict(result)
|
||||
return result
|
||||
|
||||
def request(self, url, method, run_async=False, *args, **kwargs):
|
||||
def request(
|
||||
self, url, method, run_async=False, error_message=None,
|
||||
*args, **kwargs):
|
||||
name_parts = extract_name(url)
|
||||
name = '.'.join([self.service_type, method] + name_parts)
|
||||
class_name = "".join([
|
||||
|
@ -179,4 +181,4 @@ class ShadeAdapter(adapter.Adapter):
|
|||
if run_async:
|
||||
return response
|
||||
else:
|
||||
return self._munch_response(response)
|
||||
return self._munch_response(response, error_message=error_message)
|
||||
|
|
32
shade/exc.py
32
shade/exc.py
|
@ -89,18 +89,36 @@ OpenStackCloudResourceNotFound = OpenStackCloudURINotFound
|
|||
|
||||
|
||||
# Logic shamelessly stolen from requests
|
||||
def raise_from_response(response):
|
||||
def raise_from_response(response, error_message=None):
|
||||
msg = ''
|
||||
if error_message:
|
||||
msg = _
|
||||
if 400 <= response.status_code < 500:
|
||||
msg = '({code}) Client Error: {reason} for url: {url}'.format(
|
||||
code=response.status_code,
|
||||
reason=response.reason,
|
||||
url=response.url)
|
||||
source = "Client"
|
||||
elif 500 <= response.status_code < 600:
|
||||
msg = '({code}) Server Error: {reason} for url: {url}'.format(
|
||||
code=response.status_code,
|
||||
source = "Server"
|
||||
else:
|
||||
source = None
|
||||
|
||||
if response.reason:
|
||||
remote_error = "Error: {reason} for {url}".format(
|
||||
reason=response.reason,
|
||||
url=response.url)
|
||||
else:
|
||||
remote_error = "Error for url: {url}".format(url=response.url)
|
||||
|
||||
if source:
|
||||
if error_message:
|
||||
msg = '{error_message}. ({code}) {source} {remote_error}'.format(
|
||||
error_message=error_message,
|
||||
source=source,
|
||||
code=response.status_code,
|
||||
remote_error=remote_error)
|
||||
else:
|
||||
msg = '({code}) {source} {remote_error}'.format(
|
||||
code=response.status_code,
|
||||
source=source,
|
||||
remote_error=remote_error)
|
||||
|
||||
# Special case 404 since we raised a specific one for neutron exceptions
|
||||
# before
|
||||
|
|
Loading…
Reference in New Issue