diff options
author | Kevin McCarthy <kmccarth@redhat.com> | 2007-10-18 09:46:13 -0700 |
---|---|---|
committer | Kevin McCarthy <kmccarth@redhat.com> | 2007-10-18 09:46:13 -0700 |
commit | 12047b529a7fcfcf83ab3a85d419f6af7ff9c6be (patch) | |
tree | 088bc71379baca39ef59ada32b90d230e9ae1d15 /ipa-server | |
parent | 6b0587726aa69cb1f4b1f983b4551bcd61440c1b (diff) | |
download | freeipa-12047b529a7fcfcf83ab3a85d419f6af7ff9c6be.tar.gz freeipa-12047b529a7fcfcf83ab3a85d419f6af7ff9c6be.tar.xz freeipa-12047b529a7fcfcf83ab3a85d419f6af7ff9c6be.zip |
Add an exception/error handler to the web gui.
Diffstat (limited to 'ipa-server')
-rw-r--r-- | ipa-server/ipa-gui/ipagui/controllers.py | 61 | ||||
-rw-r--r-- | ipa-server/ipa-gui/ipagui/templates/unhandled_exception.kid | 31 |
2 files changed, 92 insertions, 0 deletions
diff --git a/ipa-server/ipa-gui/ipagui/controllers.py b/ipa-server/ipa-gui/ipagui/controllers.py index 1025b6c36..5d0bfee03 100644 --- a/ipa-server/ipa-gui/ipagui/controllers.py +++ b/ipa-server/ipa-gui/ipagui/controllers.py @@ -1,8 +1,11 @@ import logging +import StringIO +import traceback import cherrypy import turbogears from turbogears import controllers, expose, flash +from turbogears import config from turbogears import validators, validate from turbogears import widgets, paginate from turbogears import error_handler @@ -41,3 +44,61 @@ class Root(controllers.RootController): @expose("ipagui.templates.loginfailed") def loginfailed(self, **kw): return dict() + + + _error_codes = { + None: u'General Error', + 400: u'400 - Bad Request', + 401: u'401 - Unauthorized', + 403: u'403 - Forbidden', + 404: u'404 - Not Found', + 500: u'500 - Internal Server Error', + 501: u'501 - Not Implemented', + 502: u'502 - Bad Gateway', + } + + def handle_error(self, status, message): + """This method is derived from the sample error catcher on + http://docs.turbogears.org/1.0/ErrorReporting.""" + try: + cherrypy._cputil._cp_on_http_error(status, message) + error_msg = self._error_codes.get(status, self._error_codes[None]) + url = "%s %s" % (cherrypy.request.method, cherrypy.request.path) + log.exception("CherryPy %s error (%s) for request '%s'", status, + error_msg, url) + + if config.get('server.environment') == 'production': + details = '' + else: + buf = StringIO.StringIO() + traceback.print_exc(file=buf) + details = buf.getvalue() + buf.close() + + data = dict( + status = status, + message = message, + error_msg = error_msg, + url = url, + details = details, + ) + + body = controllers._process_output( + data, + 'ipagui.templates.unhandled_exception', + 'html', + 'text/html', + None + ) + cherrypy.response.headers['Content-Length'] = len(body) + cherrypy.response.body = body + + # don't catch SystemExit + except StandardError, exc: + log.exception('Error handler failed: %s', exc) + + # To hook in error handler for production only: + # if config.get('server.environment') == 'production': + # _cp_on_http_error = handle_error + + _cp_on_http_error = handle_error diff --git a/ipa-server/ipa-gui/ipagui/templates/unhandled_exception.kid b/ipa-server/ipa-gui/ipagui/templates/unhandled_exception.kid new file mode 100644 index 000000000..8b6f91254 --- /dev/null +++ b/ipa-server/ipa-gui/ipagui/templates/unhandled_exception.kid @@ -0,0 +1,31 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:py="http://purl.org/kid/ns#" + py:extends="'master.kid'"> +<head> +<meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/> +<title>Error</title> +</head> + +<body> + <div id="main_content"> + <h1>An unexpected error occured</h1> + + <div py:if='message'> + <b>Message:</b> + <pre>${message}</pre> + </div> + + <div py:if='error_msg'> + <b>HTTP Error Message:</b> + <pre>${error_msg}</pre> + </div> + + <div py:if='details'> + <b>Stack Trace:</b> + <pre>${details}</pre> + </div> + </div> + +</body> +</html> |