added middleware to catch errors

This commit is contained in:
John Dickinson 2010-10-29 11:10:40 -05:00
parent 937554c85e
commit 1d3b72da48
4 changed files with 102 additions and 1 deletions
etc
setup.py
swift/common/middleware
test/unit/common/middleware

@ -9,7 +9,7 @@
# key_file = /etc/swift/proxy.key
[pipeline:main]
pipeline = healthcheck cache ratelimit auth proxy-server
pipeline = catch_errors healthcheck cache ratelimit auth proxy-server
[app:proxy-server]
use = egg:swift#proxy
@ -76,3 +76,6 @@ use = egg:swift#ratelimit
# container_ratelimit_0 = 100
# container_ratelimit_10 = 50
# container_ratelimit_50 = 20
[filter:cate_errors]
use = egg:swift#catch_errors

@ -93,6 +93,7 @@ setup(
'healthcheck=swift.common.middleware.healthcheck:filter_factory',
'memcache=swift.common.middleware.memcache:filter_factory',
'ratelimit=swift.common.middleware.ratelimit:filter_factory',
'catch_errors=swift.common.middleware.catch_errors:filter_factory',
],
},
)

@ -0,0 +1,48 @@
# Copyright (c) 2010 OpenStack, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from webob import Request
from webob.exc import HTTPServerError
from swift.common.utils import get_logger
class CatchErrorMiddleware(object):
"""
Middleware that provides high-level error handling.
"""
def __init__(self, app, conf):
self.app = app
self.logger = get_logger(conf)
def __call__(self, env, start_response):
try:
return self.app(env, start_response)
except Exception, err:
self.logger.exception('Error: %s' % err)
resp = HTTPServerError(request=Request(env),
body='An error occurred',
content_type='text/plain')
return resp(env, start_response)
def filter_factory(global_conf, **local_conf):
conf = global_conf.copy()
conf.update(local_conf)
def except_filter(app):
return CatchErrorMiddleware(app, conf)
return except_filter

@ -0,0 +1,49 @@
# Copyright (c) 2010 OpenStack, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from webob import Request
from swift.common.middleware import catch_errors
class FakeApp(object):
def __init__(self, error=False):
self.error = error
def __call__(self, env, start_response):
if self.error:
raise Exception('augh!')
return "FAKE APP"
def start_response(*args):
pass
class TestCatchErrors(unittest.TestCase):
def test_catcherrors_passthrough(self):
app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
resp = app(req.environ, start_response)
self.assertEquals(resp, 'FAKE APP')
def test_catcherrors(self):
app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
resp = app(req.environ, start_response)
self.assertEquals(resp, ['An error occurred'])
if __name__ == '__main__':
unittest.main()