]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Don't send untextified exc to webob
authorJay S. Bryant <jsbryant@us.ibm.com>
Wed, 12 Mar 2014 22:39:18 +0000 (17:39 -0500)
committerJay S. Bryant <jsbryant@us.ibm.com>
Thu, 13 Mar 2014 17:36:02 +0000 (12:36 -0500)
As part of commit cbe1d5f5e22e5f792128643e4cdd6afb2ff2b5bf I
accidentally removed the str() from exceptions that were being
passed out of OpenStack to webob.  This broke the contract with
webob which was expecting strings (text) to be sent.  This
resulted in webob doing bad things if we encountered an exception,
like trying to create a duplicate volume type.

This commit replaces the str()'s that shouldn't have been removed
with six.text_type() which should be used in place of str() to
resolve this issue.  There was one instance where a message was
instead being unicoded, this also was changed to use six.text_type.

This commit also fixes one message that should have been translated
but was not.

Change-Id: Iad6a81108f58c1bfd13397479022c4c4ad1ccc56
Closes-bug: 1290715

cinder/api/contrib/qos_specs_manage.py
cinder/api/contrib/types_manage.py
cinder/tests/api/contrib/test_types_manage.py

index 10a951f0ad6653a83262782444370d612ef8da23..1d50017190ed7e6a2e7b375a9e0b93f3af9b086c 100644 (file)
@@ -15,6 +15,7 @@
 
 """The QoS specs extension"""
 
+import six
 import webob
 
 from cinder.api import extensions
@@ -73,7 +74,7 @@ def _check_specs(context, specs_id):
     try:
         qos_specs.get_qos_specs(context, specs_id)
     except exception.NotFound as ex:
-        raise webob.exc.HTTPNotFound(explanation=unicode(ex))
+        raise webob.exc.HTTPNotFound(explanation=six.text_type(ex))
 
 
 class QoSSpecsController(wsgi.Controller):
@@ -121,19 +122,20 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.create',
                                          notifier_err)
-            raise webob.exc.HTTPBadRequest(explanation=err)
+            raise webob.exc.HTTPBadRequest(explanation=six.text_type(err))
         except exception.QoSSpecsExists as err:
             notifier_err = dict(name=name, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.create',
                                          notifier_err)
-            raise webob.exc.HTTPConflict(explanation=err)
+            raise webob.exc.HTTPConflict(explanation=six.text_type(err))
         except exception.QoSSpecsCreateFailed as err:
             notifier_err = dict(name=name, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.create',
                                          notifier_err)
-            raise webob.exc.HTTPInternalServerError(explanation=err)
+            raise webob.exc.HTTPInternalServerError(
+                explanation=six.text_type(err))
 
         return self._view_builder.detail(req, spec)
 
@@ -156,19 +158,20 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.update',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.InvalidQoSSpecs as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.update',
                                          notifier_err)
-            raise webob.exc.HTTPBadRequest(explanation=err)
+            raise webob.exc.HTTPBadRequest(explanation=six.text_type(err))
         except exception.QoSSpecsUpdateFailed as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.update',
                                          notifier_err)
-            raise webob.exc.HTTPInternalServerError(explanation=err)
+            raise webob.exc.HTTPInternalServerError(
+                explanation=six.text_type(err))
 
         return body
 
@@ -181,7 +184,7 @@ class QoSSpecsController(wsgi.Controller):
         try:
             spec = qos_specs.get_qos_specs(context, id)
         except exception.QoSSpecsNotFound as err:
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
 
         return self._view_builder.detail(req, spec)
 
@@ -208,7 +211,7 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.delete',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.QoSSpecsInUse as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
@@ -245,13 +248,13 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.delete_keys',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.QoSSpecsKeyNotFound as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.delete_keys',
                                          notifier_err)
-            raise webob.exc.HTTPBadRequest(explanation=err)
+            raise webob.exc.HTTPBadRequest(explanation=six.text_type(err))
 
         return webob.Response(status_int=202)
 
@@ -274,13 +277,14 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.associations',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.CinderException as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.associations',
                                          notifier_err)
-            raise webob.exc.HTTPInternalServerError(explanation=err)
+            raise webob.exc.HTTPInternalServerError(
+                explanation=six.text_type(err))
 
         return self._view_builder.associations(req, associates)
 
@@ -312,13 +316,13 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.associate',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.QoSSpecsNotFound as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.associate',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.InvalidVolumeType as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
@@ -327,13 +331,14 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.associate',
                                          notifier_err)
-            raise webob.exc.HTTPBadRequest(explanation=err)
+            raise webob.exc.HTTPBadRequest(explanation=six.text_type(err))
         except exception.QoSSpecsAssociateFailed as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.associate',
                                          notifier_err)
-            raise webob.exc.HTTPInternalServerError(explanation=err)
+            raise webob.exc.HTTPInternalServerError(
+                explanation=six.text_type(err))
 
         return webob.Response(status_int=202)
 
@@ -365,19 +370,20 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.disassociate',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.QoSSpecsNotFound as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.disassociate',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.QoSSpecsDisassociateFailed as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.disassociate',
                                          notifier_err)
-            raise webob.exc.HTTPInternalServerError(explanation=err)
+            raise webob.exc.HTTPInternalServerError(
+                explanation=six.text_type(err))
 
         return webob.Response(status_int=202)
 
@@ -399,13 +405,14 @@ class QoSSpecsController(wsgi.Controller):
             self._notify_qos_specs_error(context,
                                          'qos_specs.disassociate_all',
                                          notifier_err)
-            raise webob.exc.HTTPNotFound(explanation=err)
+            raise webob.exc.HTTPNotFound(explanation=six.text_type(err))
         except exception.QoSSpecsDisassociateFailed as err:
             notifier_err = dict(id=id, error_message=err)
             self._notify_qos_specs_error(context,
                                          'qos_specs.disassociate_all',
                                          notifier_err)
-            raise webob.exc.HTTPInternalServerError(explanation=err)
+            raise webob.exc.HTTPInternalServerError(
+                explanation=six.text_type(err))
 
         return webob.Response(status_int=202)
 
index 0efa744ecd5affea92d1922d9e05b110919f517f..f3ade54b3f1e513f0677718ad4060a7bcf0a12d5 100644 (file)
@@ -15,6 +15,7 @@
 
 """The volume types manage extension."""
 
+import six
 import webob
 
 from cinder.api import extensions
@@ -67,7 +68,7 @@ class VolumeTypesManageController(wsgi.Controller):
                                            'volume_type.create',
                                            notifier_err)
 
-            raise webob.exc.HTTPConflict(explanation=err)
+            raise webob.exc.HTTPConflict(explanation=six.text_type(err))
         except exception.NotFound as err:
             notifier_err = dict(volume_types=vol_type, error_message=err)
             self._notify_volume_type_error(context,
@@ -95,7 +96,7 @@ class VolumeTypesManageController(wsgi.Controller):
             self._notify_volume_type_error(context,
                                            'volume_type.delete',
                                            notifier_err)
-            msg = 'Target volume type is still in use.'
+            msg = _('Target volume type is still in use.')
             raise webob.exc.HTTPBadRequest(explanation=msg)
         except exception.NotFound as err:
             notifier_err = dict(id=id, error_message=err)
index f7b425373a53b273f158d850176d3f7c99e63ebd..ccca201d16e38fc5c36949c13a0d9631638dbe21 100644 (file)
@@ -54,6 +54,10 @@ def return_volume_types_create(context, name, specs):
     pass
 
 
+def return_volume_types_create_duplicate_type(context, name, specs):
+    raise exception.VolumeTypeExists(id=name)
+
+
 def return_volume_types_get_by_name(context, name):
     if name == "777":
         raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
@@ -122,6 +126,18 @@ class VolumeTypesManageApiTest(test.TestCase):
         self.assertEqual(1, len(res_dict))
         self.assertEqual('vol_type_1', res_dict['volume_type']['name'])
 
+    def test_create_duplicate_type_fail(self):
+        self.stubs.Set(volume_types, 'create',
+                       return_volume_types_create_duplicate_type)
+        self.stubs.Set(volume_types, 'get_volume_type_by_name',
+                       return_volume_types_get_by_name)
+
+        body = {"volume_type": {"name": "vol_type_1",
+                                "extra_specs": {"key1": "value1"}}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types')
+        self.assertRaises(webob.exc.HTTPConflict,
+                          self.controller._create, req, body)
+
     def _create_volume_type_bad_body(self, body):
         req = fakes.HTTPRequest.blank('/v2/fake/types')
         req.method = 'POST'