Merge lp:~notmyname/swift/except_middleware into lp:~hudson-openstack/swift/trunk

Proposed by John Dickinson
Status: Merged
Approved by: gholt
Approved revision: 116
Merged at revision: 117
Proposed branch: lp:~notmyname/swift/except_middleware
Merge into: lp:~hudson-openstack/swift/trunk
Diff against target: 139 lines (+103/-1)
4 files modified
etc/proxy-server.conf-sample (+5/-1)
setup.py (+1/-0)
swift/common/middleware/catch_errors.py (+48/-0)
test/unit/common/middleware/test_except.py (+49/-0)
To merge this branch: bzr merge lp:~notmyname/swift/except_middleware
Reviewer Review Type Date Requested Status
gholt (community) Approve
Review via email: mp+39639@code.launchpad.net

Commit message

Middleware that catches and logs errors that would otherwise be sent to the client

Description of the change

Middleware that catches and logs errors that would otherwise be sent to the client

To post a comment you must log in.
113. By John Dickinson

merged with trunk

114. By John Dickinson

merged with trunk

115. By John Dickinson

fixed typo in example config

116. By John Dickinson

pep8

Revision history for this message
gholt (gholt) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'etc/proxy-server.conf-sample'
2--- etc/proxy-server.conf-sample 2010-10-29 03:41:38 +0000
3+++ etc/proxy-server.conf-sample 2010-11-03 18:26:08 +0000
4@@ -9,7 +9,7 @@
5 # key_file = /etc/swift/proxy.key
6
7 [pipeline:main]
8-pipeline = healthcheck cache ratelimit auth proxy-server
9+pipeline = catch_errors healthcheck cache ratelimit auth proxy-server
10
11 [app:proxy-server]
12 use = egg:swift#proxy
13@@ -81,3 +81,7 @@
14 use = egg:swift#domain_remap
15 # storage_domain = example.com
16 # path_root = v1
17+
18+[filter:catch_errors]
19+use = egg:swift#catch_errors
20+
21
22=== modified file 'setup.py'
23--- setup.py 2010-10-29 03:41:38 +0000
24+++ setup.py 2010-11-03 18:26:08 +0000
25@@ -93,6 +93,7 @@
26 'healthcheck=swift.common.middleware.healthcheck:filter_factory',
27 'memcache=swift.common.middleware.memcache:filter_factory',
28 'ratelimit=swift.common.middleware.ratelimit:filter_factory',
29+ 'catch_errors=swift.common.middleware.catch_errors:filter_factory',
30 'domain_remap=swift.common.middleware.domain_remap:filter_factory',
31 ],
32 },
33
34=== added file 'swift/common/middleware/catch_errors.py'
35--- swift/common/middleware/catch_errors.py 1970-01-01 00:00:00 +0000
36+++ swift/common/middleware/catch_errors.py 2010-11-03 18:26:08 +0000
37@@ -0,0 +1,48 @@
38+# Copyright (c) 2010 OpenStack, LLC.
39+#
40+# Licensed under the Apache License, Version 2.0 (the "License");
41+# you may not use this file except in compliance with the License.
42+# You may obtain a copy of the License at
43+#
44+# http://www.apache.org/licenses/LICENSE-2.0
45+#
46+# Unless required by applicable law or agreed to in writing, software
47+# distributed under the License is distributed on an "AS IS" BASIS,
48+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
49+# implied.
50+# See the License for the specific language governing permissions and
51+# limitations under the License.
52+
53+from webob import Request
54+from webob.exc import HTTPServerError
55+
56+from swift.common.utils import get_logger
57+
58+
59+class CatchErrorMiddleware(object):
60+ """
61+ Middleware that provides high-level error handling.
62+ """
63+
64+ def __init__(self, app, conf):
65+ self.app = app
66+ self.logger = get_logger(conf)
67+
68+ def __call__(self, env, start_response):
69+ try:
70+ return self.app(env, start_response)
71+ except Exception, err:
72+ self.logger.exception('Error: %s' % err)
73+ resp = HTTPServerError(request=Request(env),
74+ body='An error occurred',
75+ content_type='text/plain')
76+ return resp(env, start_response)
77+
78+
79+def filter_factory(global_conf, **local_conf):
80+ conf = global_conf.copy()
81+ conf.update(local_conf)
82+
83+ def except_filter(app):
84+ return CatchErrorMiddleware(app, conf)
85+ return except_filter
86
87=== added file 'test/unit/common/middleware/test_except.py'
88--- test/unit/common/middleware/test_except.py 1970-01-01 00:00:00 +0000
89+++ test/unit/common/middleware/test_except.py 2010-11-03 18:26:08 +0000
90@@ -0,0 +1,49 @@
91+# Copyright (c) 2010 OpenStack, LLC.
92+#
93+# Licensed under the Apache License, Version 2.0 (the "License");
94+# you may not use this file except in compliance with the License.
95+# You may obtain a copy of the License at
96+#
97+# http://www.apache.org/licenses/LICENSE-2.0
98+#
99+# Unless required by applicable law or agreed to in writing, software
100+# distributed under the License is distributed on an "AS IS" BASIS,
101+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
102+# implied.
103+# See the License for the specific language governing permissions and
104+# limitations under the License.
105+
106+import unittest
107+
108+from webob import Request
109+
110+from swift.common.middleware import catch_errors
111+
112+class FakeApp(object):
113+ def __init__(self, error=False):
114+ self.error = error
115+
116+ def __call__(self, env, start_response):
117+ if self.error:
118+ raise Exception('augh!')
119+ return "FAKE APP"
120+
121+def start_response(*args):
122+ pass
123+
124+class TestCatchErrors(unittest.TestCase):
125+
126+ def test_catcherrors_passthrough(self):
127+ app = catch_errors.CatchErrorMiddleware(FakeApp(), {})
128+ req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
129+ resp = app(req.environ, start_response)
130+ self.assertEquals(resp, 'FAKE APP')
131+
132+ def test_catcherrors(self):
133+ app = catch_errors.CatchErrorMiddleware(FakeApp(True), {})
134+ req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
135+ resp = app(req.environ, start_response)
136+ self.assertEquals(resp, ['An error occurred'])
137+
138+if __name__ == '__main__':
139+ unittest.main()