diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index e668f5e8c9..28c2102836 100644 --- a/etc/proxy-server.conf-sample +++ b/etc/proxy-server.conf-sample @@ -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 @@ -82,6 +82,9 @@ use = egg:swift#domain_remap # storage_domain = example.com # path_root = v1 +[filter:catch_errors] +use = egg:swift#catch_errors + [filter:cname_lookup] use = egg:swift#cname_lookup # storage_domain = example.com diff --git a/setup.py b/setup.py index 2b7c14d12a..f72517f0de 100644 --- a/setup.py +++ b/setup.py @@ -94,6 +94,7 @@ setup( 'memcache=swift.common.middleware.memcache:filter_factory', 'ratelimit=swift.common.middleware.ratelimit:filter_factory', 'cname_lookup=swift.common.middleware.cname_lookup:filter_factory', + 'catch_errors=swift.common.middleware.catch_errors:filter_factory', 'domain_remap=swift.common.middleware.domain_remap:filter_factory', ], }, diff --git a/swift/common/middleware/catch_errors.py b/swift/common/middleware/catch_errors.py new file mode 100644 index 0000000000..e94133627e --- /dev/null +++ b/swift/common/middleware/catch_errors.py @@ -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 diff --git a/test/unit/common/middleware/test_except.py b/test/unit/common/middleware/test_except.py new file mode 100644 index 0000000000..25e9486ab0 --- /dev/null +++ b/test/unit/common/middleware/test_except.py @@ -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()