From: Jay S. Bryant Date: Wed, 12 Mar 2014 22:39:18 +0000 (-0500) Subject: Don't send untextified exc to webob X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=5757c857bdf526dc82cc8c1d4200d92c183e9374;p=openstack-build%2Fcinder-build.git Don't send untextified exc to webob 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 --- diff --git a/cinder/api/contrib/qos_specs_manage.py b/cinder/api/contrib/qos_specs_manage.py index 10a951f0a..1d5001719 100644 --- a/cinder/api/contrib/qos_specs_manage.py +++ b/cinder/api/contrib/qos_specs_manage.py @@ -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) diff --git a/cinder/api/contrib/types_manage.py b/cinder/api/contrib/types_manage.py index 0efa744ec..f3ade54b3 100644 --- a/cinder/api/contrib/types_manage.py +++ b/cinder/api/contrib/types_manage.py @@ -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) diff --git a/cinder/tests/api/contrib/test_types_manage.py b/cinder/tests/api/contrib/test_types_manage.py index f7b425373..ccca201d1 100644 --- a/cinder/tests/api/contrib/test_types_manage.py +++ b/cinder/tests/api/contrib/test_types_manage.py @@ -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'