]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Externalize error messages in the v2 API
authorLuis A. Garcia <luis@linux.vnet.ibm.com>
Wed, 10 Jul 2013 00:50:12 +0000 (00:50 +0000)
committerLuis A. Garcia <luis@linux.vnet.ibm.com>
Thu, 15 Aug 2013 14:20:46 +0000 (14:20 +0000)
This patch does more internationalization for the REST API error
messages that don't currently have it to take advantage of the new
support added by bp user-locale-api to show error messages in the locale
requested by the user through the Accept-Language HTTP header.

We only do v2 because consumers have used the response error message in
the past for error checks, so changing it in v1 too would break them.

Partially implements bp user-locale-api

Change-Id: I92780b42c125a91ab4916b7a31e4b71d306a89a1

cinder/api/v2/snapshots.py
cinder/api/v2/types.py
cinder/api/v2/volumes.py

index 8e348c65a73b27a0c0a38589c6d8f854a5277bd8..5e415392b0791b328e0e4bc1d0dae3719522b765 100644 (file)
@@ -108,7 +108,8 @@ class SnapshotsController(wsgi.Controller):
         try:
             vol = self.volume_api.get_snapshot(context, id)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Snapshot could not be found")
+            raise exc.HTTPNotFound(explanation=msg)
 
         return {'snapshot': _translate_snapshot_detail_view(context, vol)}
 
@@ -122,7 +123,9 @@ class SnapshotsController(wsgi.Controller):
             snapshot = self.volume_api.get_snapshot(context, id)
             self.volume_api.delete_snapshot(context, snapshot)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Snapshot could not be found")
+            raise exc.HTTPNotFound(explanation=msg)
+
         return webob.Response(status_int=202)
 
     @wsgi.serializers(xml=SnapshotsTemplate)
@@ -168,7 +171,9 @@ class SnapshotsController(wsgi.Controller):
         context = req.environ['cinder.context']
 
         if not self.is_valid_body(body, 'snapshot'):
-            raise exc.HTTPBadRequest()
+            msg = (_("Missing required element '%s' in request body") %
+                   'snapshot')
+            raise exc.HTTPBadRequest(explanation=msg)
 
         snapshot = body['snapshot']
         kwargs['metadata'] = snapshot.get('metadata', None)
@@ -213,10 +218,13 @@ class SnapshotsController(wsgi.Controller):
         context = req.environ['cinder.context']
 
         if not body:
-            raise exc.HTTPBadRequest()
+            msg = _("Missing request body")
+            raise exc.HTTPBadRequest(explanation=msg)
 
         if 'snapshot' not in body:
-            raise exc.HTTPBadRequest()
+            msg = (_("Missing required element '%s' in request body") %
+                   'snapshot')
+            raise exc.HTTPBadRequest(explanation=msg)
 
         snapshot = body['snapshot']
         update_dict = {}
@@ -246,7 +254,8 @@ class SnapshotsController(wsgi.Controller):
             snapshot = self.volume_api.get_snapshot(context, id)
             self.volume_api.update_snapshot(context, snapshot, update_dict)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Snapshot could not be found")
+            raise exc.HTTPNotFound(explanation=msg)
 
         snapshot.update(update_dict)
 
index 1513b8dcfa4ccd608495739dc791cdf04c8faaec..7d11edbb35c44f51c17ea688dc1fc5af6c681b02 100644 (file)
@@ -69,7 +69,8 @@ class VolumeTypesController(wsgi.Controller):
         try:
             vol_type = volume_types.get_volume_type(context, id)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Volume type not found")
+            raise exc.HTTPNotFound(explanation=msg)
 
         # TODO(bcwaldon): remove str cast once we use uuids
         vol_type['id'] = str(vol_type['id'])
index c2b3f938ffc9cb44863e2213b51db90d2ca72a82..f9169914e5e6dea11dcb482004227fb625c1ea2e 100644 (file)
@@ -165,7 +165,8 @@ class VolumeController(wsgi.Controller):
         try:
             vol = self.volume_api.get(context, id)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Volume could not be found")
+            raise exc.HTTPNotFound(explanation=msg)
 
         return self._view_builder.detail(req, vol)
 
@@ -179,10 +180,11 @@ class VolumeController(wsgi.Controller):
             volume = self.volume_api.get(context, id)
             self.volume_api.delete(context, volume)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Volume could not be found")
+            raise exc.HTTPNotFound(explanation=msg)
         except exception.VolumeAttached:
-            explanation = 'Volume cannot be deleted while in attached state'
-            raise exc.HTTPBadRequest(explanation=explanation)
+            msg = _("Volume cannot be deleted while in attached state")
+            raise exc.HTTPBadRequest(explanation=msg)
         return webob.Response(status_int=202)
 
     @wsgi.serializers(xml=VolumesTemplate)
@@ -250,7 +252,8 @@ class VolumeController(wsgi.Controller):
     def create(self, req, body):
         """Creates a new volume."""
         if not self.is_valid_body(body, 'volume'):
-            raise exc.HTTPBadRequest()
+            msg = _("Missing required element '%s' in request body") % 'volume'
+            raise exc.HTTPBadRequest(explanation=msg)
 
         LOG.debug('Create volume request body: %s', body)
         context = req.environ['cinder.context']
@@ -274,8 +277,8 @@ class VolumeController(wsgi.Controller):
                 kwargs['volume_type'] = volume_types.get_volume_type(
                     context, req_volume_type)
             except exception.VolumeTypeNotFound:
-                explanation = 'Volume type not found.'
-                raise exc.HTTPNotFound(explanation=explanation)
+                msg = _("Volume type not found")
+                raise exc.HTTPNotFound(explanation=msg)
 
         kwargs['metadata'] = volume.get('metadata', None)
 
@@ -335,10 +338,12 @@ class VolumeController(wsgi.Controller):
         context = req.environ['cinder.context']
 
         if not body:
-            raise exc.HTTPBadRequest()
+            msg = _("Missing request body")
+            raise exc.HTTPBadRequest(explanation=msg)
 
         if 'volume' not in body:
-            raise exc.HTTPBadRequest()
+            msg = _("Missing required element '%s' in request body") % 'volume'
+            raise exc.HTTPBadRequest(explanation=msg)
 
         volume = body['volume']
         update_dict = {}
@@ -367,7 +372,8 @@ class VolumeController(wsgi.Controller):
             volume = self.volume_api.get(context, id)
             self.volume_api.update(context, volume, update_dict)
         except exception.NotFound:
-            raise exc.HTTPNotFound()
+            msg = _("Volume could not be found")
+            raise exc.HTTPNotFound(explanation=msg)
 
         volume.update(update_dict)