summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHe Jie Xu <hejie.xu@intel.com>2017-05-18 10:52:54 +0800
committerHe Jie Xu <hejie.xu@intel.com>2017-05-19 00:37:09 +0800
commit5e51fed4a0f75ec6c97cd2582d89584b9364ccfc (patch)
tree4c3e29158fb339c356ca0627de4e5c4fcc140547
parent8d492c76d53f3fcfacdd945a277446bdfe6797b0 (diff)
downloadnova-5e51fed4a0f75ec6c97cd2582d89584b9364ccfc.tar.gz
Correct the error message for query parameter validation
Currently the error message returned by failed query parameter validation didn't include correct parameter name. This patch correct that. Change-Id: Ifd0b37084709c2728749b5b7b317f304b0488269 Closes-Bug: #1655710
-rw-r--r--nova/api/validation/validators.py21
-rw-r--r--nova/tests/unit/test_api_validation.py9
2 files changed, 23 insertions, 7 deletions
diff --git a/nova/api/validation/validators.py b/nova/api/validation/validators.py
index 6ac478755d..f3c0c9d1a7 100644
--- a/nova/api/validation/validators.py
+++ b/nova/api/validation/validators.py
@@ -280,14 +280,23 @@ class _SchemaValidator(object):
# message has been written as the similar format of
# WSME.
detail = _("Invalid input for field/attribute %(path)s. "
- "Value: %(value)s. %(message)s")
+ "Value: %(value)s. %(message)s") % {
+ 'path': ex.path.pop(),
+ 'value': ex.instance,
+ 'message': ex.message}
else:
+ # NOTE: Use 'ex.path.popleft()' instead of 'ex.path.pop()',
+ # due to the structure of query parameters is a dict
+ # with key as name and value is list. So the first
+ # item in the 'ex.path' is the key, and second item
+ # is the index of list in the value. We need the
+ # key as the parameter name in the error message.
+ # So pop the first value out of 'ex.path'.
detail = _("Invalid input for query parameters %(path)s. "
- "Value: %(value)s. %(message)s")
- detail = detail % {
- 'path': ex.path.pop(), 'value': ex.instance,
- 'message': ex.message
- }
+ "Value: %(value)s. %(message)s") % {
+ 'path': ex.path.popleft(),
+ 'value': ex.instance,
+ 'message': ex.message}
else:
detail = ex.message
raise exception.ValidationError(detail=detail)
diff --git a/nova/tests/unit/test_api_validation.py b/nova/tests/unit/test_api_validation.py
index eab625c89a..e1861323cf 100644
--- a/nova/tests/unit/test_api_validation.py
+++ b/nova/tests/unit/test_api_validation.py
@@ -271,7 +271,14 @@ class QueryParamsSchemaTestCase(test.NoDBTestCase):
# parameter 'foo' expect a UUID
req = fakes.HTTPRequest.blank("/tests?foo=abc")
req.api_version_request = api_version.APIVersionRequest("2.3")
- self.assertRaises(exception.ValidationError, self.controller.get, req)
+ ex = self.assertRaises(exception.ValidationError, self.controller.get,
+ req)
+ if six.PY3:
+ self.assertEqual("Invalid input for query parameters foo. Value: "
+ "abc. 'abc' is not a 'uuid'", ex.message)
+ else:
+ self.assertEqual("Invalid input for query parameters foo. Value: "
+ "abc. u'abc' is not a 'uuid'", ex.message)
def test_validate_request_with_multiple_values(self):
req = fakes.HTTPRequest.blank("/tests?foos=abc")