From: Zane Bitter Date: Thu, 17 Jan 2013 10:10:14 +0000 (+0100) Subject: Prepare the groundwork for more exception types X-Git-Tag: 2014.1~1002 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=819113b22611ce8f7780ef678792aada4e9be412;p=openstack-build%2Fheat-build.git Prepare the groundwork for more exception types We have been abusing AttributeError and ValueError rather than raising exceptions that actually describe the problem. This opens the way for changes that will allow us to handle a wide variety of specific exceptions. Change-Id: I05c99601d4e877878195e658908fffa0e126f06c Signed-off-by: Zane Bitter --- diff --git a/heat/api/aws/exception.py b/heat/api/aws/exception.py index 60712082..9dca5ec2 100644 --- a/heat/api/aws/exception.py +++ b/heat/api/aws/exception.py @@ -243,10 +243,13 @@ def map_remote_error(ex): to HeatAPIException subclasses which can be used to return properly formatted AWS error responses """ - if ex.exc_type in ('AttributeError', 'ValueError'): - # Attribute/Value error, bad user data, ex.value should tell us why + inval_param_errors = ( + 'AttributeError', + 'ValueError', + ) + + if ex.exc_type in inval_param_errors: return HeatInvalidParameterValueError(detail=ex.value) else: # Map everything else to internal server error for now - # FIXME : further investigation into engine errors required return HeatInternalFailureError(detail=ex.value) diff --git a/heat/api/openstack/v1/util.py b/heat/api/openstack/v1/util.py index 3b358dfd..73eb8444 100644 --- a/heat/api/openstack/v1/util.py +++ b/heat/api/openstack/v1/util.py @@ -86,10 +86,13 @@ def remote_error(ex, force_exists=False): 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)) + client_error = exc.HTTPBadRequest if force_exists else exc.HTTPNotFound + error_map = { + 'AttributeError': client_error, + 'ValueError': client_error, + } + + Exc = error_map.get(ex.exc_type, exc.HTTPInternalServerError) + + raise Exc(explanation=str(ex)) diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index 5037aada..1c3b077d 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -20,6 +20,7 @@ import mox from nose.plugins.attrib import attr from heat.common import context +from heat.common import exception from heat.tests.v1_1 import fakes import heat.engine.api as engine_api import heat.db as db_api