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
20
class ErrorHandlerApp(object):
21
"""Class for WSGI error logging middleware."""
25
def __init__(self, application, **kwargs):
26
self.application = application
28
def __call__(self, environ, start_response):
30
return self.application(environ, start_response)
32
# test if exc_info has been set, in the case that
33
# the error is caused before BranchWSGGIApp middleware
34
if environ.has_key('exc_info') and environ.has_key('branch'):
35
# Log and/or report any application errors
36
return self.handle_error(environ, start_response)
38
# simply propagate the error, this is logged
39
# by paste.httpexceptions.TransLogger middleware
42
def handle_error(self, environ, start_response):
43
"""Exception hanlder."""
44
self.log_error(environ)
45
return errapp(environ, start_response)
47
def log_error(self, environ):
48
exc_type, exc_object, exc_tb = environ['exc_info']
49
logger = environ['branch'].log
50
logger.exception(self.msg, exc_type.__module__,
51
exc_type.__name__, exc_object)
54
def errapp(environ, start_response):
55
'''Default (and trivial) error handling WSGI application.'''
56
c = ErrorUI(environ['branch'], environ['exc_info'])
57
return c(environ, start_response)