add bare except to catch errors

Change-Id: Ibe78912cf923591bddd6a8cf0e683cd028c9c4e8
This commit is contained in:
John Dickinson 2013-11-20 17:15:19 -08:00 committed by Peter Portante
parent 9e25d38611
commit cdb6cd830a
2 changed files with 14 additions and 3 deletions

View File

@ -14,7 +14,6 @@
# limitations under the License. # limitations under the License.
from swift import gettext_ as _ from swift import gettext_ as _
from eventlet import Timeout
from swift.common.swob import Request, HTTPServerError from swift.common.swob import Request, HTTPServerError
from swift.common.utils import get_logger, generate_trans_id from swift.common.utils import get_logger, generate_trans_id
@ -35,8 +34,8 @@ class CatchErrorsContext(WSGIContext):
try: try:
# catch any errors in the pipeline # catch any errors in the pipeline
resp = self._app_call(env) resp = self._app_call(env)
except (Exception, Timeout) as err: except: # noqa
self.logger.exception(_('Error: %s'), err) self.logger.exception(_('Error: An error occurred'))
resp = HTTPServerError(request=Request(env), resp = HTTPServerError(request=Request(env),
body='An error occurred', body='An error occurred',
content_type='text/plain') content_type='text/plain')

View File

@ -20,6 +20,10 @@ from swift.common.middleware import catch_errors
from swift.common.utils import get_logger from swift.common.utils import get_logger
class StrangeException(BaseException):
pass
class FakeApp(object): class FakeApp(object):
def __init__(self, error=False, body_iter=None): def __init__(self, error=False, body_iter=None):
@ -30,6 +34,8 @@ class FakeApp(object):
if 'swift.trans_id' not in env: if 'swift.trans_id' not in env:
raise Exception('Trans id should always be in env') raise Exception('Trans id should always be in env')
if self.error: if self.error:
if self.error == 'strange':
raise StrangeException('whoa')
raise Exception('An error occurred') raise Exception('An error occurred')
if self.body_iter is None: if self.body_iter is None:
return ["FAKE APP"] return ["FAKE APP"]
@ -97,6 +103,12 @@ class TestCatchErrors(unittest.TestCase):
app(req.environ, start_response) app(req.environ, start_response)
self.assertTrue(self.logger.txn_id.endswith('-stuff')) self.assertTrue(self.logger.txn_id.endswith('-stuff'))
def test_catcherrors_with_unexpected_error(self):
app = catch_errors.CatchErrorMiddleware(FakeApp(error='strange'), {})
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'})
resp = app(req.environ, start_response)
self.assertEquals(list(resp), ['An error occurred'])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()