merged with trunk
This commit is contained in:
commit
559fc110a7
@ -9,7 +9,7 @@
|
|||||||
# key_file = /etc/swift/proxy.key
|
# key_file = /etc/swift/proxy.key
|
||||||
|
|
||||||
[pipeline:main]
|
[pipeline:main]
|
||||||
pipeline = healthcheck cache ratelimit auth proxy-server
|
pipeline = catch_errors healthcheck cache ratelimit auth proxy-server
|
||||||
|
|
||||||
[app:proxy-server]
|
[app:proxy-server]
|
||||||
use = egg:swift#proxy
|
use = egg:swift#proxy
|
||||||
@ -82,6 +82,9 @@ use = egg:swift#domain_remap
|
|||||||
# storage_domain = example.com
|
# storage_domain = example.com
|
||||||
# path_root = v1
|
# path_root = v1
|
||||||
|
|
||||||
|
[filter:catch_errors]
|
||||||
|
use = egg:swift#catch_errors
|
||||||
|
|
||||||
[filter:cname_lookup]
|
[filter:cname_lookup]
|
||||||
use = egg:swift#cname_lookup
|
use = egg:swift#cname_lookup
|
||||||
# storage_domain = example.com
|
# storage_domain = example.com
|
||||||
|
1
setup.py
1
setup.py
@ -94,6 +94,7 @@ setup(
|
|||||||
'memcache=swift.common.middleware.memcache:filter_factory',
|
'memcache=swift.common.middleware.memcache:filter_factory',
|
||||||
'ratelimit=swift.common.middleware.ratelimit:filter_factory',
|
'ratelimit=swift.common.middleware.ratelimit:filter_factory',
|
||||||
'cname_lookup=swift.common.middleware.cname_lookup: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',
|
'domain_remap=swift.common.middleware.domain_remap:filter_factory',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
48
swift/common/middleware/catch_errors.py
Normal file
48
swift/common/middleware/catch_errors.py
Normal file
@ -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
|
49
test/unit/common/middleware/test_except.py
Normal file
49
test/unit/common/middleware/test_except.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user