From: Luis A. Garcia Date: Wed, 10 Jul 2013 00:50:12 +0000 (+0000) Subject: Externalize error messages in the v2 API X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=681a898101f81dbe2317a84e4496bd1e000cf527;p=openstack-build%2Fcinder-build.git Externalize error messages in the v2 API 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 --- diff --git a/cinder/api/v2/snapshots.py b/cinder/api/v2/snapshots.py index 8e348c65a..5e415392b 100644 --- a/cinder/api/v2/snapshots.py +++ b/cinder/api/v2/snapshots.py @@ -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) diff --git a/cinder/api/v2/types.py b/cinder/api/v2/types.py index 1513b8dcf..7d11edbb3 100644 --- a/cinder/api/v2/types.py +++ b/cinder/api/v2/types.py @@ -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']) diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index c2b3f938f..f9169914e 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -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)