]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Avoid serializing CinderExceptions before they are translated
authorLuis A. Garcia <luis@linux.vnet.ibm.com>
Mon, 19 Aug 2013 20:19:57 +0000 (20:19 +0000)
committerLuis A. Garcia <luis@linux.vnet.ibm.com>
Mon, 19 Aug 2013 23:05:43 +0000 (23:05 +0000)
CinderExceptions were being unicode()'d when being wrapped in an
HTTPException, and this was causing the delayed translation to fail for
those errors.

Also, CinderExceptions have a 'message' class attribute that holds the
generic error message template, e.g. "Backup  %(backup_id)s is not
found", unfortunately, because the names are the same, it was
overshadowing the actual exception instance 'message', e.g. "Backup 1 is
not found", when translating. This patch puts the exception's actual
message in a new field called 'msg'.

Fixes Bug: #1214102

Change-Id: Ied9abcc3d05454852c0a5891432eb181220a744e

cinder/api/contrib/backups.py
cinder/api/contrib/types_extra_specs.py
cinder/api/contrib/volume_actions.py
cinder/api/contrib/volume_transfer.py
cinder/api/contrib/volume_type_encryption.py
cinder/api/openstack/wsgi.py
cinder/api/v1/snapshot_metadata.py
cinder/api/v1/volume_metadata.py
cinder/api/v2/snapshot_metadata.py
cinder/exception.py
cinder/tests/api/contrib/test_backups.py

index 6c67b2db752af11b3ee356f13a57b2758c6ab40a..e284baa29ddf1eb8210cddbba9ada3c7b2a0ce35 100644 (file)
@@ -131,7 +131,7 @@ class BackupsController(wsgi.Controller):
         try:
             backup = self.backup_api.get(context, backup_id=id)
         except exception.BackupNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
 
         return self._view_builder.detail(req, backup)
 
@@ -145,9 +145,9 @@ class BackupsController(wsgi.Controller):
         try:
             self.backup_api.delete(context, id)
         except exception.BackupNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
         except exception.InvalidBackup as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
 
         return webob.Response(status_int=202)
 
@@ -207,11 +207,11 @@ class BackupsController(wsgi.Controller):
             new_backup = self.backup_api.create(context, name, description,
                                                 volume_id, container)
         except exception.InvalidVolume as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
         except exception.VolumeNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
         except exception.ServiceNotFound as error:
-            raise exc.HTTPInternalServerError(explanation=unicode(error))
+            raise exc.HTTPInternalServerError(explanation=error.msg)
 
         retval = self._view_builder.summary(req, dict(new_backup.iteritems()))
         return retval
@@ -244,21 +244,21 @@ class BackupsController(wsgi.Controller):
                                                   backup_id=id,
                                                   volume_id=volume_id)
         except exception.InvalidInput as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
         except exception.InvalidVolume as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
         except exception.InvalidBackup as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
         except exception.BackupNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
         except exception.VolumeNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
         except exception.VolumeSizeExceedsAvailableQuota as error:
             raise exc.HTTPRequestEntityTooLarge(
-                explanation=error.message, headers={'Retry-After': 0})
+                explanation=error.msg, headers={'Retry-After': 0})
         except exception.VolumeLimitExceeded as error:
             raise exc.HTTPRequestEntityTooLarge(
-                explanation=error.message, headers={'Retry-After': 0})
+                explanation=error.msg, headers={'Retry-After': 0})
 
         retval = self._view_builder.restore_summary(
             req, dict(new_restore.iteritems()))
index 0887c912fd6663de60a736e09b5eddef74ff8334..95314db4f4757c69dd3cdb36ba9d37dda2fe4ec5 100644 (file)
@@ -64,7 +64,7 @@ class VolumeTypeExtraSpecsController(wsgi.Controller):
         try:
             volume_types.get_volume_type(context, type_id)
         except exception.NotFound as ex:
-            raise webob.exc.HTTPNotFound(explanation=unicode(ex))
+            raise webob.exc.HTTPNotFound(explanation=ex.msg)
 
     @wsgi.serializers(xml=VolumeTypeExtraSpecsTemplate)
     def index(self, req, type_id):
@@ -138,7 +138,7 @@ class VolumeTypeExtraSpecsController(wsgi.Controller):
         try:
             db.volume_type_extra_specs_delete(context, type_id, id)
         except exception.VolumeTypeExtraSpecsNotFound as error:
-            raise webob.exc.HTTPNotFound(explanation=unicode(error))
+            raise webob.exc.HTTPNotFound(explanation=error.msg)
 
         notifier_info = dict(type_id=type_id, id=id)
         notifier_api.notify(context, 'volumeTypeExtraSpecs',
index 63d45246b935756b2a564e6ca75fe28f5c02fda0..7bec44930523432e1ac99f59793864a0e1121aa9 100644 (file)
@@ -183,7 +183,7 @@ class VolumeActionsController(wsgi.Controller):
         try:
             volume = self.volume_api.get(context, id)
         except exception.VolumeNotFound as error:
-            raise webob.exc.HTTPNotFound(explanation=unicode(error))
+            raise webob.exc.HTTPNotFound(explanation=error.msg)
         authorize(context, "upload_image")
         image_metadata = {"container_format": params.get("container_format",
                                                          "bare"),
@@ -195,7 +195,7 @@ class VolumeActionsController(wsgi.Controller):
                                                             image_metadata,
                                                             force)
         except exception.InvalidVolume as error:
-            raise webob.exc.HTTPBadRequest(explanation=unicode(error))
+            raise webob.exc.HTTPBadRequest(explanation=error.msg)
         except ValueError as error:
             raise webob.exc.HTTPBadRequest(explanation=unicode(error))
         except rpc_common.RemoteError as error:
index e21cd05f2b4c3632c3a6a0697bca927d6ffeae6b..a400f05ef4c5ce3bca30c73614c8010043705853 100644 (file)
@@ -113,7 +113,7 @@ class VolumeTransferController(wsgi.Controller):
         try:
             transfer = self.transfer_api.get(context, transfer_id=id)
         except exception.TransferNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
 
         return self._view_builder.detail(req, transfer)
 
@@ -168,9 +168,9 @@ class VolumeTransferController(wsgi.Controller):
         try:
             new_transfer = self.transfer_api.create(context, volume_id, name)
         except exception.InvalidVolume as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
         except exception.VolumeNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
 
         transfer = self._view_builder.create(req,
                                              dict(new_transfer.iteritems()))
@@ -203,9 +203,9 @@ class VolumeTransferController(wsgi.Controller):
                                                          auth_key)
         except exception.VolumeSizeExceedsAvailableQuota as error:
             raise exc.HTTPRequestEntityTooLarge(
-                explanation=error.message, headers={'Retry-After': 0})
+                explanation=error.msg, headers={'Retry-After': 0})
         except exception.InvalidVolume as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
 
         transfer = \
             self._view_builder.summary(req,
@@ -221,7 +221,7 @@ class VolumeTransferController(wsgi.Controller):
         try:
             self.transfer_api.delete(context, transfer_id=id)
         except exception.TransferNotFound as error:
-            raise exc.HTTPNotFound(explanation=unicode(error))
+            raise exc.HTTPNotFound(explanation=error.msg)
         return webob.Response(status_int=202)
 
 
index aede70f9f169256ef9fd04315148019e3c0113dd..d8aed1575d6fad2140e8b7f33051a5acf916e643 100644 (file)
@@ -55,7 +55,7 @@ class VolumeTypeEncryptionController(wsgi.Controller):
         try:
             volume_types.get_volume_type(context, type_id)
         except exception.NotFound as ex:
-            raise webob.exc.HTTPNotFound(explanation=unicode(ex))
+            raise webob.exc.HTTPNotFound(explanation=ex.msg)
 
     def _check_encryption_input(self, encryption, create=True):
         if 'key_size' in encryption.keys():
index 89a4356ae417966477a7c59dc7bcab2d531ef6a7..5ce6f76f3647863e0a5d7fc28900462c0be5526b 100644 (file)
@@ -588,11 +588,10 @@ class ResourceExceptionHandler(object):
             return True
 
         if isinstance(ex_value, exception.NotAuthorized):
-            msg = unicode(ex_value)
-            raise Fault(webob.exc.HTTPForbidden(explanation=msg))
+            raise Fault(webob.exc.HTTPForbidden(explanation=ex_value.msg))
         elif isinstance(ex_value, exception.Invalid):
             raise Fault(exception.ConvertedException(
-                code=ex_value.code, explanation=unicode(ex_value)))
+                code=ex_value.code, explanation=ex_value.msg))
         elif isinstance(ex_value, TypeError):
             exc_info = (ex_type, ex_value, ex_traceback)
             LOG.error(_(
index 32922c4b2b2410aae8802a8ab96c4daefbcd13a4..9328d138b8872de5a797bce097ed761f9048dac3 100644 (file)
@@ -124,10 +124,10 @@ class Controller(object):
             raise exc.HTTPBadRequest(explanation=msg)
 
         except exception.InvalidVolumeMetadata as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
 
         except exception.InvalidVolumeMetadataSize as error:
-            raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
+            raise exc.HTTPRequestEntityTooLarge(explanation=error.msg)
 
     @wsgi.serializers(xml=common.MetaItemTemplate)
     def show(self, req, snapshot_id, id):
index 7065dfa3fab71914724e9f7ad10fcb3bfd24d017..64589774597a357092bf1bfd6eeb58765bafc3dc 100644 (file)
@@ -124,10 +124,10 @@ class Controller(object):
             raise exc.HTTPBadRequest(explanation=msg)
 
         except exception.InvalidVolumeMetadata as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
 
         except exception.InvalidVolumeMetadataSize as error:
-            raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
+            raise exc.HTTPRequestEntityTooLarge(explanation=error.msg)
 
     @wsgi.serializers(xml=common.MetaItemTemplate)
     def show(self, req, volume_id, id):
index 32922c4b2b2410aae8802a8ab96c4daefbcd13a4..9328d138b8872de5a797bce097ed761f9048dac3 100644 (file)
@@ -124,10 +124,10 @@ class Controller(object):
             raise exc.HTTPBadRequest(explanation=msg)
 
         except exception.InvalidVolumeMetadata as error:
-            raise exc.HTTPBadRequest(explanation=unicode(error))
+            raise exc.HTTPBadRequest(explanation=error.msg)
 
         except exception.InvalidVolumeMetadataSize as error:
-            raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
+            raise exc.HTTPRequestEntityTooLarge(explanation=error.msg)
 
     @wsgi.serializers(xml=common.MetaItemTemplate)
     def show(self, req, snapshot_id, id):
index 6af8a2d87d740836489b1f6c8f5c1d3f8d9ff6c9..1d6141345bffffe92b7dc5ee38a2542efec417c6 100644 (file)
@@ -119,8 +119,15 @@ class CinderException(Exception):
                 # at least get the core message out if something happened
                 message = self.message
 
+        # NOTE(luisg): We put the actual message in 'msg' so that we can access
+        # it, because if we try to access the message via 'message' it will be
+        # overshadowed by the class' message attribute
+        self.msg = message
         super(CinderException, self).__init__(message)
 
+    def __unicode__(self):
+        return unicode(self.msg)
+
 
 class GlanceConnectionFailed(CinderException):
     message = _("Connection to glance failed") + ": %(reason)s"
index a7c5c73fd1d3334005ae24ded74aea828b5959ce..fdc57aabc31e49568cc46c99181c006d946bf0fe 100644 (file)
@@ -914,8 +914,7 @@ class BackupsAPITestCase(test.TestCase):
         self.assertEqual(res.status_int, 413)
         self.assertEqual(res_dict['overLimit']['code'], 413)
         self.assertEqual(res_dict['overLimit']['message'],
-                         'Maximum number of volumes allowed '
-                         '(%(allowed)d) exceeded')
+                         'Maximum number of volumes allowed (1) exceeded')
 
     def test_restore_backup_to_undersized_volume(self):
         backup_size = 10