summaryrefslogtreecommitdiff
path: root/glanceclient/exc.py
diff options
context:
space:
mode:
authord34dh0r53 <david.wilde@rackspace.com>2015-01-19 18:02:08 -0600
committerd34dh0r53 <david.wilde@rackspace.com>2015-02-16 10:01:53 -0600
commit01caf4e734fb012f0b5b19c24a8ffeaef31a06be (patch)
tree72a6d61aa4f95a8b0f2bb8393cbf14b367847f97 /glanceclient/exc.py
parentaebbcff100f5a07e64506d72fbd4427e725085a9 (diff)
downloadpython-glanceclient-01caf4e734fb012f0b5b19c24a8ffeaef31a06be.tar.gz
Strip json and html from error messages
Error messages were being passed with either JSON or HTML formatting leading to an unpleasant user experience. This strips the JSON or HTML tags and presents the clean error message to the user. Rewrote the lispy function to be more pythonic, added test cases and cleaned up some pep8 violations. Removed commented out cruft from a previous version of this patch. Changed the HTML details from a set to a list in order to ensure the ordering of the exception details. Added code to eliminate the duplicate detail messages from the list and reordered the assertion string to match actual output. Refactored duplicate elimination code to be more readable and reliable. Some reworking of the duplication elimination loop to make it more readable. Removed unneeded conditional to filter out empty elements. Change-Id: I79985b3e305cb30328a3c16b025315a8e969243d Closes-Bug: 1398838
Diffstat (limited to 'glanceclient/exc.py')
-rw-r--r--glanceclient/exc.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/glanceclient/exc.py b/glanceclient/exc.py
index 3eeaffa..95eb575 100644
--- a/glanceclient/exc.py
+++ b/glanceclient/exc.py
@@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+import re
import sys
@@ -141,7 +142,7 @@ class HTTPServiceUnavailable(ServiceUnavailable):
pass
-#NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception
+# NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception
# classes
_code_map = {}
for obj_name in dir(sys.modules[__name__]):
@@ -153,7 +154,29 @@ for obj_name in dir(sys.modules[__name__]):
def from_response(response, body=None):
"""Return an instance of an HTTPException based on httplib response."""
cls = _code_map.get(response.status_code, HTTPException)
- if body:
+ if body and 'json' in response.headers['content-type']:
+ # Iterate over the nested objects and retreive the "message" attribute.
+ messages = [obj.get('message') for obj in response.json().values()]
+ # Join all of the messages together nicely and filter out any objects
+ # that don't have a "message" attr.
+ details = '\n'.join(i for i in messages if i is not None)
+ return cls(details=details)
+ elif body and 'html' in response.headers['content-type']:
+ # Split the lines, strip whitespace and inline HTML from the response.
+ details = [re.sub(r'<.+?>', '', i.strip())
+ for i in response.text.splitlines()]
+ details = [i for i in details if i]
+ # Remove duplicates from the list.
+ details_seen = set()
+ details_temp = []
+ for i in details:
+ if i not in details_seen:
+ details_temp.append(i)
+ details_seen.add(i)
+ # Return joined string separated by colons.
+ details = ': '.join(details_temp)
+ return cls(details=details)
+ elif body:
details = body.replace('\n\n', '\n')
return cls(details=details)