]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
ReST API: Move remote error handler to utils module
authorZane Bitter <zbitter@redhat.com>
Fri, 16 Nov 2012 09:58:34 +0000 (10:58 +0100)
committerZane Bitter <zbitter@redhat.com>
Fri, 16 Nov 2012 10:51:56 +0000 (11:51 +0100)
Change-Id: Id3b99c62c01f1c22aa39518bb0f650d41f5975f0
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/api/openstack/v1/stacks.py
heat/api/openstack/v1/util.py

index a2f962c49c53c2e09134d41273f3f6b08e2ac45f..fc4b0e5a11eb2d077699698d3d7f90de5fad8b92 100644 (file)
@@ -160,20 +160,6 @@ class StackController(object):
         self.options = options
         self.engine_rpcapi = engine_rpcapi.EngineAPI()
 
-    def _remote_error(self, ex, force_exists=False):
-        """
-        Map rpc_common.RemoteError exceptions returned by the engine
-        to webob exceptions which can be used to return
-        properly formatted error responses.
-        """
-        if ex.exc_type in ('AttributeError', 'ValueError'):
-            if force_exists:
-                raise exc.HTTPBadRequest(explanation=str(ex))
-            else:
-                raise exc.HTTPNotFound(explanation=str(ex))
-
-        raise exc.HTTPInternalServerError(explanation=str(ex))
-
     def default(self, req, **args):
         raise exc.HTTPNotFound()
 
@@ -186,7 +172,7 @@ class StackController(object):
         try:
             stack_list = self.engine_rpcapi.list_stacks(req.context)
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex, True)
+            return util.remote_error(ex, True)
 
         summary_keys = (engine_api.STACK_ID,
                         engine_api.STACK_NAME,
@@ -216,7 +202,7 @@ class StackController(object):
                                                      data.user_params(),
                                                      data.args())
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex, True)
+            return util.remote_error(ex, True)
 
         if 'Description' in result:
             raise exc.HTTPBadRequest(explanation=result['Description'])
@@ -233,7 +219,7 @@ class StackController(object):
             identity = self.engine_rpcapi.identify_stack(req.context,
                                                          stack_name)
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex)
+            return util.remote_error(ex)
 
         raise exc.HTTPFound(location=util.make_url(req, identity))
 
@@ -247,7 +233,7 @@ class StackController(object):
             stack_list = self.engine_rpcapi.show_stack(req.context,
                                                        identity)
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex)
+            return util.remote_error(ex)
 
         if not stack_list['stacks']:
             raise exc.HTTPInternalServerError()
@@ -266,7 +252,7 @@ class StackController(object):
             templ = self.engine_rpcapi.get_template(req.context,
                                                     identity)
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex)
+            return util.remote_error(ex)
 
         if templ is None:
             raise exc.HTTPNotFound()
@@ -288,7 +274,7 @@ class StackController(object):
                                                   data.user_params(),
                                                   data.args())
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex)
+            return util.remote_error(ex)
 
         if 'Description' in res:
             raise exc.HTTPBadRequest(explanation=res['Description'])
@@ -307,7 +293,7 @@ class StackController(object):
                                                   cast=False)
 
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex)
+            return util.remote_error(ex)
 
         if res is not None:
             raise exc.HTTPBadRequest(explanation=res['Error'])
@@ -327,7 +313,7 @@ class StackController(object):
             result = self.engine_rpcapi.validate_template(req.context,
                                                           data.template())
         except rpc_common.RemoteError as ex:
-            return self._remote_error(ex, True)
+            return util.remote_error(ex, True)
 
         if 'Error' in result:
             raise exc.HTTPBadRequest(explanation=result['Error'])
index 1b333b21249629437d5c66b67af1eb44d9775ee6..26de4c01a0d1f6dc001ed37cc4a1747d6da7d5a8 100644 (file)
@@ -13,6 +13,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from webob import exc
 from functools import wraps
 
 from heat.engine import identifier
@@ -61,3 +62,18 @@ def make_url(req, identity):
 def make_link(req, identity, relationship='self'):
     '''Return a link structure for the supplied identity dictionary.'''
     return {'href': make_url(req, identity), 'rel': relationship}
+
+
+def remote_error(ex, force_exists=False):
+    """
+    Map rpc_common.RemoteError exceptions returned by the engine
+    to webob exceptions which can be used to return
+    properly formatted error responses.
+    """
+    if ex.exc_type in ('AttributeError', 'ValueError'):
+        if force_exists:
+            raise exc.HTTPBadRequest(explanation=str(ex))
+        else:
+            raise exc.HTTPNotFound(explanation=str(ex))
+
+    raise exc.HTTPInternalServerError(explanation=str(ex))