Merge "add bare except to catch errors"
This commit is contained in:
commit
2ce0e62c31
@ -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')
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user