19
19
from swift.common.swob import Request, HTTPServerError
20
20
from swift.common.utils import get_logger
23
class CatchErrorMiddleware(object):
25
Middleware that provides high-level error handling and ensures that a
26
transaction id will be set for every request.
29
def __init__(self, app, conf):
31
self.logger = get_logger(conf, log_route='catch-errors')
33
def __call__(self, env, start_response):
35
If used, this should be the first middleware in pipeline.
21
from swift.common.wsgi import WSGIContext
24
class CatchErrorsContext(WSGIContext):
26
def __init__(self, app, logger):
27
super(CatchErrorsContext, self).__init__(app)
30
def handle_request(self, env, start_response):
37
31
trans_id = 'tx' + uuid.uuid4().hex
38
32
env['swift.trans_id'] = trans_id
39
33
self.logger.txn_id = trans_id
42
def my_start_response(status, response_headers, exc_info=None):
43
trans_header = ('x-trans-id', trans_id)
44
response_headers.append(trans_header)
45
return start_response(status, response_headers, exc_info)
46
return self.app(env, my_start_response)
35
# catch any errors in the pipeline
36
resp = self._app_call(env)
47
37
except (Exception, Timeout), err:
48
38
self.logger.exception(_('Error: %s'), err)
49
39
resp = HTTPServerError(request=Request(env),
52
42
resp.headers['x-trans-id'] = trans_id
53
43
return resp(env, start_response)
45
# make sure the response has the trans_id
46
if self._response_headers is None:
47
self._response_headers = []
48
self._response_headers.append(('x-trans-id', trans_id))
49
start_response(self._response_status, self._response_headers,
50
self._response_exc_info)
54
class CatchErrorMiddleware(object):
56
Middleware that provides high-level error handling and ensures that a
57
transaction id will be set for every request.
60
def __init__(self, app, conf):
62
self.logger = get_logger(conf, log_route='catch-errors')
64
def __call__(self, env, start_response):
66
If used, this should be the first middleware in pipeline.
68
context = CatchErrorsContext(self.app, self.logger)
69
return context.handle_request(env, start_response)
56
72
def filter_factory(global_conf, **local_conf):
57
73
conf = global_conf.copy()