From 1d3b72da488808f6ecffdc234fd9ced56412834c Mon Sep 17 00:00:00 2001 From: John Dickinson Date: Fri, 29 Oct 2010 11:10:40 -0500 Subject: [PATCH 1/3] added middleware to catch errors --- etc/proxy-server.conf-sample | 5 ++- setup.py | 1 + swift/common/middleware/catch_errors.py | 48 +++++++++++++++++++++ test/unit/common/middleware/test_except.py | 49 ++++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 swift/common/middleware/catch_errors.py create mode 100644 test/unit/common/middleware/test_except.py diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index aef2aec700..0314c4d4c4 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 @@ -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 \ No newline at end of file diff --git a/setup.py b/setup.py index 66f4956cc6..62b4b45427 100644 --- a/setup.py +++ b/setup.py @@ -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', ], }, ) diff --git a/swift/common/middleware/catch_errors.py b/swift/common/middleware/catch_errors.py new file mode 100644 index 0000000000..6ef32d0c99 --- /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() From f89b8ca22fd1e1037ff41018625616e2366a2164 Mon Sep 17 00:00:00 2001 From: John Dickinson Date: Wed, 3 Nov 2010 13:17:59 -0500 Subject: [PATCH 2/3] fixed typo in example config --- etc/proxy-server.conf-sample | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index a5327f0df7..be41a7fcb6 100644 --- a/etc/proxy-server.conf-sample +++ b/etc/proxy-server.conf-sample @@ -82,6 +82,6 @@ use = egg:swift#domain_remap # storage_domain = example.com # path_root = v1 -[filter:cate_errors] +[filter:catch_errors] use = egg:swift#catch_errors From 926804eb86dfdfb1d3abb8538480380d780c7073 Mon Sep 17 00:00:00 2001 From: John Dickinson Date: Wed, 3 Nov 2010 13:23:17 -0500 Subject: [PATCH 3/3] pep8 --- swift/common/middleware/catch_errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swift/common/middleware/catch_errors.py b/swift/common/middleware/catch_errors.py index 6ef32d0c99..e94133627e 100644 --- a/swift/common/middleware/catch_errors.py +++ b/swift/common/middleware/catch_errors.py @@ -42,7 +42,7 @@ class CatchErrorMiddleware(object): 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