Merge "Add support for parsing jsonfied heat error"

This commit is contained in:
Jenkins 2013-08-30 19:55:45 +00:00 committed by Gerrit Code Review
commit 21f477d29f
2 changed files with 64 additions and 13 deletions

View File

@ -31,21 +31,28 @@ LOG = logging.getLogger(__name__)
def exception_to_validation_msg(e):
'''
Extracts a validation message to display to the user.
This needs to be a pattern matching approach until the Heat
API returns exception data in a parsable format.
'''
validation_patterns = [
"Remote error: \w* {'Error': '(.*?)'}",
'Remote error: \w* (.*?) \[',
'400 Bad Request\n\nThe server could not comply with the request '
'since it is either malformed or otherwise incorrect.\n\n (.*)',
'(ParserError: .*)'
]
try:
error = json.loads(str(e))
# NOTE(jianingy): if no message exists, we just return 'None'
# and let the caller to deciede what to show
return error['error'].get('message', None)
except Exception:
# NOTE(jianingy): fallback to legacy message parsing approach
# either if error message isn't a json nor the json isn't in
# valid format.
validation_patterns = [
"Remote error: \w* {'Error': '(.*?)'}",
'Remote error: \w* (.*?) \[',
'400 Bad Request\n\nThe server could not comply with the request '
'since it is either malformed or otherwise incorrect.\n\n (.*)',
'(ParserError: .*)'
]
for pattern in validation_patterns:
match = re.search(pattern, str(e))
if match:
return match.group(1)
for pattern in validation_patterns:
match = re.search(pattern, str(e))
if match:
return match.group(1)
class TemplateForm(forms.SelfHandlingForm):

View File

@ -154,3 +154,47 @@ class StackTests(test.TestCase):
'method': forms.StackCreateForm.__name__}
res = self.client.post(url, form_data)
self.assertRedirectsNoFollow(res, INDEX_URL)
class TemplateFormTests(test.TestCase):
def test_exception_to_validation(self):
json_error = """{
"code": 400,
"error": {
"message": "The Key (none) could not be found.",
"traceback": "<Traceback>",
"type": "StackValidationFailed"
},
"explanation": "The server could not comply with the request",
"title": "Bad Request"
}"""
msg = forms.exception_to_validation_msg(json_error)
self.assertEqual(msg, "The Key (none) could not be found.")
def test_exception_to_validation_legacy(self):
json_error = """400 Bad Request
The server could not comply with the request since it is either \
malformed or otherwise incorrect.
Remote error: StackValidationFailed The Key (none) could not be found. \
[u'<Traceback>']."""
msg = forms.exception_to_validation_msg(json_error)
self.assertEqual(msg, "The Key (none) could not be found.")
def test_exception_to_validation_malformed(self):
json_error = """{
"code": 400,
"error": {
"traceback": "<Traceback>",
"type": "StackValidationFailed"
},
"explanation": "The server could not comply with the request",
"title": "Bad Request"
}"""
msg = forms.exception_to_validation_msg(json_error)
self.assertEqual(msg, None)