~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to swift/common/middleware/catch_errors.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-28 09:40:30 UTC
  • mfrom: (1.2.16)
  • Revision ID: package-import@ubuntu.com-20130128094030-aetz57x2qz9ye2d4
Tags: 1.7.6-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from swift.common.swob import Request, HTTPServerError
20
20
from swift.common.utils import get_logger
21
 
 
22
 
 
23
 
class CatchErrorMiddleware(object):
24
 
    """
25
 
    Middleware that provides high-level error handling and ensures that a
26
 
    transaction id will be set for every request.
27
 
    """
28
 
 
29
 
    def __init__(self, app, conf):
30
 
        self.app = app
31
 
        self.logger = get_logger(conf, log_route='catch-errors')
32
 
 
33
 
    def __call__(self, env, start_response):
34
 
        """
35
 
        If used, this should be the first middleware in pipeline.
36
 
        """
 
21
from swift.common.wsgi import WSGIContext
 
22
 
 
23
 
 
24
class CatchErrorsContext(WSGIContext):
 
25
 
 
26
    def __init__(self, app, logger):
 
27
        super(CatchErrorsContext, self).__init__(app)
 
28
        self.logger = logger
 
29
 
 
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
40
34
        try:
41
 
 
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)
54
44
 
 
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)
 
51
        return resp
 
52
 
 
53
 
 
54
class CatchErrorMiddleware(object):
 
55
    """
 
56
    Middleware that provides high-level error handling and ensures that a
 
57
    transaction id will be set for every request.
 
58
    """
 
59
 
 
60
    def __init__(self, app, conf):
 
61
        self.app = app
 
62
        self.logger = get_logger(conf, log_route='catch-errors')
 
63
 
 
64
    def __call__(self, env, start_response):
 
65
        """
 
66
        If used, this should be the first middleware in pipeline.
 
67
        """
 
68
        context = CatchErrorsContext(self.app, self.logger)
 
69
        return context.handle_request(env, start_response)
 
70
 
55
71
 
56
72
def filter_factory(global_conf, **local_conf):
57
73
    conf = global_conf.copy()