1
# Copyright (C) 2008 Guillermo Gonzalez <guillo.gonzo@gmail.com>
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.
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.
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
18
from loggerhead.controllers.error_ui import ErrorUI
21
class ErrorHandlerApp(object):
22
"""Class for WSGI error logging middleware."""
26
def __init__(self, application, **kwargs):
27
self.application = application
29
def __call__(self, environ, start_response):
31
return self.application(environ, start_response)
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)
39
# simply propagate the error, this is logged
40
# by paste.httpexceptions.TransLogger middleware
43
def handle_error(self, environ, start_response):
44
"""Exception hanlder."""
45
self.log_error(environ)
46
return errapp(environ, start_response)
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)
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)