]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Externalize error messages in the API
authorLuis A. Garcia <luis@linux.vnet.ibm.com>
Wed, 31 Jul 2013 16:04:31 +0000 (16:04 +0000)
committerLuis A. Garcia <luis@linux.vnet.ibm.com>
Wed, 7 Aug 2013 21:19:25 +0000 (21:19 +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.

Partially implements bp user-locale-api

Change-Id: I92780b42c125a91ab4916b7a31e4b71d306a89a1

neutron/api/extensions.py
neutron/api/v2/base.py
neutron/api/versions.py
neutron/extensions/quotasv2.py
neutron/quota.py
neutron/wsgi.py

index 2f315baeace551aa776a4c24ede145489a93d685..15d3f7ecf792139f824736d9285b18c5c8fca62b 100644 (file)
@@ -249,10 +249,12 @@ class ExtensionController(wsgi.Controller):
         return dict(extension=self._translate(ext))
 
     def delete(self, request, id):
-        raise webob.exc.HTTPNotFound()
+        msg = _('Resource not found.')
+        raise webob.exc.HTTPNotFound(msg)
 
     def create(self, request):
-        raise webob.exc.HTTPNotFound()
+        msg = _('Resource not found.')
+        raise webob.exc.HTTPNotFound(msg)
 
 
 class ExtensionMiddleware(wsgi.Middleware):
index 3ea70a82cb714ab64975739e427870c1c79ec193..b9cbd19d871a05ed250bb44d820098f5ec73de17 100644 (file)
@@ -170,7 +170,8 @@ class Controller(object):
                 try:
                     resource = self._item(request, id, True)
                 except exceptions.PolicyNotAuthorized:
-                    raise webob.exc.HTTPNotFound()
+                    msg = _('The resource could not be found.')
+                    raise webob.exc.HTTPNotFound(msg)
                 body = kwargs.pop('body', None)
                 # Explicit comparison with None to distinguish from {}
                 if body is not None:
@@ -291,7 +292,8 @@ class Controller(object):
         except exceptions.PolicyNotAuthorized:
             # To avoid giving away information, pretend that it
             # doesn't exist
-            raise webob.exc.HTTPNotFound()
+            msg = _('The resource could not be found.')
+            raise webob.exc.HTTPNotFound(msg)
 
     def _emulate_bulk_create(self, obj_creator, request, body, parent_id=None):
         objs = []
@@ -423,7 +425,8 @@ class Controller(object):
         except exceptions.PolicyNotAuthorized:
             # To avoid giving away information, pretend that it
             # doesn't exist
-            raise webob.exc.HTTPNotFound()
+            msg = _('The resource could not be found.')
+            raise webob.exc.HTTPNotFound(msg)
 
         obj_deleter = getattr(self._plugin, action)
         obj_deleter(request.context, id, **kwargs)
@@ -473,7 +476,8 @@ class Controller(object):
         except exceptions.PolicyNotAuthorized:
             # To avoid giving away information, pretend that it
             # doesn't exist
-            raise webob.exc.HTTPNotFound()
+            msg = _('The resource could not be found.')
+            raise webob.exc.HTTPNotFound(msg)
 
         obj_updater = getattr(self._plugin, action)
         kwargs = {self._resource: body}
index f99f0b8f4e70310edc0077f4b4b579da4e071918..5707b34873ffab134a1e09a5c6cc9b6879dae550 100644 (file)
@@ -18,6 +18,7 @@
 import webob.dec
 
 from neutron.api.views import versions as versions_view
+from neutron.openstack.common import gettextutils
 from neutron.openstack.common import log as logging
 from neutron import wsgi
 
@@ -42,7 +43,10 @@ class Versions(object):
         ]
 
         if req.path != '/':
-            return webob.exc.HTTPNotFound()
+            language = req.best_match_language()
+            msg = _('Unknown API version specified')
+            msg = gettextutils.get_localized_message(msg, language)
+            return webob.exc.HTTPNotFound(explanation=msg)
 
         builder = versions_view.get_view_builder(req)
         versions = [builder.build(version) for version in version_objs]
index ba27c42ddf4549e04708bee8417fea64c57f86df..41a7b5622fcea574c00fae318f9ed33c010a4f9e 100644 (file)
@@ -66,7 +66,8 @@ class QuotaSetsController(wsgi.Controller):
             request.context, QUOTAS.resources, tenant_id)
 
     def create(self, request, body=None):
-        raise webob.exc.HTTPNotImplemented()
+        msg = _('POST requests are not supported on this resource.')
+        raise webob.exc.HTTPNotImplemented(msg)
 
     def index(self, request):
         context = request.context
index 098a1599de11a0bfb30a12eca80874825f1b7f69..d2d6a759550391d61beb8d6b76b8ee292768055f 100644 (file)
@@ -143,11 +143,13 @@ class ConfDriver(object):
 
     @staticmethod
     def delete_tenant_quota(context, tenant_id):
-        raise webob.exc.HTTPForbidden()
+        msg = _('Access to this resource was denied.')
+        raise webob.exc.HTTPForbidden(msg)
 
     @staticmethod
     def update_quota_limit(context, tenant_id, resource, limit):
-        raise webob.exc.HTTPForbidden()
+        msg = _('Access to this resource was denied.')
+        raise webob.exc.HTTPForbidden(msg)
 
 
 class BaseResource(object):
index 029b7bdfe2f50a25d99fa962f3955af160b505c2..35c97465607e37292c8c6c5657056c2a69210738 100644 (file)
@@ -952,7 +952,7 @@ class Router(object):
         return self._router
 
     @staticmethod
-    @webob.dec.wsgify
+    @webob.dec.wsgify(RequestClass=Request)
     def _dispatch(req):
         """Dispatch a Request.
 
@@ -962,7 +962,10 @@ class Router(object):
         """
         match = req.environ['wsgiorg.routing_args'][1]
         if not match:
-            return webob.exc.HTTPNotFound()
+            language = req.best_match_language()
+            msg = _('The resource could not be found.')
+            msg = gettextutils.get_localized_message(msg, language)
+            return webob.exc.HTTPNotFound(explanation=msg)
         app = match['controller']
         return app
 
@@ -1167,7 +1170,8 @@ class Controller(object):
         try:
             return serializer.serialize(data, content_type)
         except exception.InvalidContentType:
-            raise webob.exc.HTTPNotAcceptable()
+            msg = _('The requested content type %s is invalid.') % content_type
+            raise webob.exc.HTTPNotAcceptable(msg)
 
     def _deserialize(self, data, content_type):
         """Deserialize the request body to the specefied content type.