~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/apps/error.py

  • Committer: webmaster at geoffish
  • Date: 2011-07-10 22:53:59 UTC
  • mto: (459.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 461.
  • Revision ID: webmaster@geoffish.tk-20110710225359-oeairailjy6gzpuk
Fixed buggy merging and removed IDE files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008  Guillermo Gonzalez <guillo.gonzo@gmail.com>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
#
 
17
 
 
18
from loggerhead.controllers.error_ui import ErrorUI
 
19
 
 
20
 
 
21
class ErrorHandlerApp(object):
 
22
    """Class for WSGI error logging middleware."""
 
23
 
 
24
    msg = " %s.%s: %s \n"
 
25
 
 
26
    def __init__(self, application, **kwargs):
 
27
        self.application = application
 
28
 
 
29
    def __call__(self, environ, start_response):
 
30
        try:
 
31
            return self.application(environ, start_response)
 
32
        except:
 
33
            # test if exc_info has been set, in the case that
 
34
            # the error is caused before BranchWSGGIApp middleware
 
35
            if 'exc_info' in environ.keys() and 'branch' in environ.keys():
 
36
                # Log and/or report any application errors
 
37
                return self.handle_error(environ, start_response)
 
38
            else:
 
39
                # simply propagate the error, this is logged
 
40
                # by paste.httpexceptions.TransLogger middleware
 
41
                raise
 
42
 
 
43
    def handle_error(self, environ, start_response):
 
44
        """Exception hanlder."""
 
45
        self.log_error(environ)
 
46
        return errapp(environ, start_response)
 
47
 
 
48
    def log_error(self, environ):
 
49
        exc_type, exc_object, exc_tb = environ['exc_info']
 
50
        logger = environ['branch'].log
 
51
        logger.exception(self.msg, exc_type.__module__,
 
52
                              exc_type.__name__, exc_object)
 
53
 
 
54
 
 
55
def errapp(environ, start_response):
 
56
    """Default (and trivial) error handling WSGI application."""
 
57
    c = ErrorUI(environ['branch'], environ['exc_info'])
 
58
    return c(environ, start_response)