Merge pull request #149 from rnirmal/bugfix

Adding a fault wrapper to catch and wrap exceptions that are thrown as p...
This commit is contained in:
Nirmal Ranganathan 2012-06-25 15:37:47 -07:00
commit 55eb6d1e59
4 changed files with 61 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()