diff --git a/etc/reddwarf/reddwarf.conf.sample b/etc/reddwarf/reddwarf.conf.sample index 264956d03c..f2412dd87a 100644 --- a/etc/reddwarf/reddwarf.conf.sample +++ b/etc/reddwarf/reddwarf.conf.sample @@ -86,7 +86,7 @@ use = call:reddwarf.common.wsgi:versioned_urlmap paste.app_factory = reddwarf.versions:app_factory [pipeline:reddwarfapi] -pipeline = tokenauth authorization contextwrapper extensions reddwarfapp +pipeline = faultwrapper tokenauth authorization contextwrapper extensions reddwarfapp #pipeline = debug extensions reddwarfapp [filter:extensions] @@ -109,6 +109,9 @@ paste.filter_factory = reddwarf.common.auth:AuthorizationMiddleware.factory [filter:contextwrapper] paste.filter_factory = reddwarf.common.wsgi:ContextMiddleware.factory +[filter:faultwrapper] +paste.filter_factory = reddwarf.common.wsgi:FaultWrapper.factory + [app:reddwarfapp] paste.app_factory = reddwarf.common.api:app_factory diff --git a/etc/reddwarf/reddwarf.conf.test b/etc/reddwarf/reddwarf.conf.test index 8b7622ddbf..ee93deac5d 100644 --- a/etc/reddwarf/reddwarf.conf.test +++ b/etc/reddwarf/reddwarf.conf.test @@ -94,7 +94,7 @@ use = call:reddwarf.common.wsgi:versioned_urlmap paste.app_factory = reddwarf.versions:app_factory [pipeline:reddwarfapi] -pipeline = tokenauth authorization contextwrapper extensions reddwarfapp +pipeline = faultwrapper tokenauth authorization contextwrapper extensions reddwarfapp #pipeline = debug extensions reddwarfapp [filter:extensions] @@ -117,6 +117,9 @@ paste.filter_factory = reddwarf.common.auth:AuthorizationMiddleware.factory [filter:contextwrapper] paste.filter_factory = reddwarf.common.wsgi:ContextMiddleware.factory +[filter:faultwrapper] +paste.filter_factory = reddwarf.common.wsgi:FaultWrapper.factory + [app:reddwarfapp] paste.app_factory = reddwarf.common.api:app_factory diff --git a/reddwarf/common/wsgi.py b/reddwarf/common/wsgi.py index 5392095c96..3f856a770f 100644 --- a/reddwarf/common/wsgi.py +++ b/reddwarf/common/wsgi.py @@ -313,6 +313,15 @@ class ReddwarfResponseSerializer(openstack_wsgi.ResponseSerializer): class Fault(webob.exc.HTTPException): """Error codes for API faults.""" + code_wrapper = { + 400: webob.exc.HTTPBadRequest, + 401: webob.exc.HTTPUnauthorized, + 403: webob.exc.HTTPUnauthorized, + 404: webob.exc.HTTPNotFound, + } + + resp_codes = [int(code) for code in code_wrapper.keys()] + def __init__(self, exception): """Create a Fault for the given webob.exc.exception.""" @@ -407,3 +416,29 @@ class ContextMiddleware(openstack_wsgi.Middleware): local_config) return cls(app) return _factory + + +class FaultWrapper(openstack_wsgi.Middleware): + """Calls down the middleware stack, making exceptions into faults.""" + + @webob.dec.wsgify(RequestClass=openstack_wsgi.Request) + def __call__(self, req): + try: + resp = req.get_response(self.application) + if resp.status_int in Fault.resp_codes: + for (header, value) in resp._headerlist: + if header == "Content-Type" and \ + value == "text/plain; charset=UTF-8": + return Fault(Fault.code_wrapper[resp.status_int]()) + return resp + return resp + except Exception as ex: + LOG.exception(_("Caught error: %s"), unicode(ex)) + exc = webob.exc.HTTPInternalServerError() + return Fault(exc) + + @classmethod + def factory(cls, global_config, **local_config): + def _factory(app): + return cls(app) + return _factory diff --git a/reddwarf/extensions/mysql/service.py b/reddwarf/extensions/mysql/service.py index f921434f67..46fbee72e8 100644 --- a/reddwarf/extensions/mysql/service.py +++ b/reddwarf/extensions/mysql/service.py @@ -131,11 +131,17 @@ class UserController(BaseController): LOG.info(_("Deleting user for instance '%s'") % instance_id) LOG.info(_("req : '%s'\n\n") % req) context = req.environ[wsgi.CONTEXT_KEY] - user = guest_models.MySQLUser() - user.name = id - models.User.delete(context, instance_id, user.serialize()) + try: + user = guest_models.MySQLUser() + user.name = id + models.User.delete(context, instance_id, user.serialize()) + except ValueError as ve: + raise exception.BadRequest(ve.message) return wsgi.Result(None, 202) + def show(self, req, tenant_id, instance_id, id): + raise webob.exc.HTTPNotImplemented() + class SchemaController(BaseController): """Controller for instance functionality""" @@ -178,7 +184,13 @@ class SchemaController(BaseController): LOG.info(_("Deleting schema for instance '%s'") % instance_id) LOG.info(_("req : '%s'\n\n") % req) context = req.environ[wsgi.CONTEXT_KEY] - schema = guest_models.MySQLDatabase() - schema.name = id - models.Schema.delete(context, instance_id, schema.serialize()) + try: + schema = guest_models.MySQLDatabase() + schema.name = id + models.Schema.delete(context, instance_id, schema.serialize()) + except ValueError as ve: + raise exception.BadRequest(ve.message) return wsgi.Result(None, 202) + + def show(self, req, tenant_id, instance_id, id): + raise webob.exc.HTTPNotImplemented()