view MoinMoin/ @ 52:80b3d4e891ff

merge moin--main--1.3--patch-931: fix hidding of huge tracebacks Patches applied: * fix hidding of huge tracebacks * cgitb: fix attribute quoting * failure: hide huge tracebacks correctly * merge from main imported from: moin--main--1.5--patch-53
author Nir Soffer <>
date Thu, 29 Sep 2005 00:39:21 +0000
parents cf52f97a5857
children 141083b64fb1
line wrap: on
line source

# -*- coding: iso-8859-1 -*-
""" MoinMoin failure

Handle fatal errors by showing a message and debugging information.

@copyright: 2004, 2005 by Nir Soffer <>
@license: GNU GPL, see COPYING for details.
import sys, os
from import cgitb

class View(cgitb.View):
    """ Display an error message and debugging information
    Additions to cgitb.View:
     - Multiple tracebacks support
     - Debugging information is shown only in debug mode
     - Moin application information
     - General help text and links
     - Handle multiple paragraphs in exception message
    cgitb is heavily modified cgitb, but fully backward compatible with
    the standard cgitb. It should not contain any moin specific code.  

    cgitb was refactored to be easy to customize by applications
    developers. This moin specific subclass is an example.
    debugInfoID = 'debug-info'
    def formatContent(self):
        content = (
        return ''.join(content)
    def script(self):
        return '''
<script type="text/javascript">
function toggleDebugInfo() {
    var tb = document.getElementById('%s');
    if (tb == null) return; = ? '' : 'none';
''' % self.debugInfoID

    def stylesheet(self):
        return cgitb.View.stylesheet(self) + """
.cgitb .buttons {margin: 0.5em 0; padding: 5px 10px;}        
.cgitb .buttons li {display: inline; margin: 0; padding: 0 0.25em;}

    def formatMessage(self):
        """ handle multiple paragraphs messages and add general help """
        f = self.formatter
        text = [self.formatExceptionMessage(,
                f.paragraph("If you want to report a bug, please save "
                            "this page and  attach it to your bug report."),]
        return ''.join(text)

    def formatButtons(self):
        """ Add 'buttons' to the error dialog """
        f = self.formatter
        buttons = ['javascript:toggleDebugInfo()', 
                          'Show debugging information'),
                          'Report bug'),
                          'Visit MoinMoin wiki'),]
        return f.list(buttons, {'class': 'buttons'})
    def formatDebugInfo(self):
        """ Put debugging information in a hidden div """
        attributes = {'id': self.debugInfoID}
        info = [self.debugInfoHideScript(),
        return self.formatter.section(''.join(info), attributes)

    def debugInfoHideScript(self):
        """ Hide debug info for javascript enabled browsers """
        if self.debug:
            return ''
        return '''
<script type="text/javascript">toggleDebugInfo()</script>

    def formatTraceback(self):
        return self.formatAllTracebacks(self.formatOneTraceback)
    def formatTextTraceback(self):
        template = self.textTracebackTemplate()
        return template % self.formatAllTracebacks(self.formatOneTextTraceback)        

    def formatAllTracebacks(self, formatFuction):
        """ Format multiple tracebacks using formatFunction """
        tracebacks = []
        for type, value, tb in self.exceptions():
            if type is None: 
            tracebacks.append(formatFuction((type, value, tb)))
            del tb
        return ''.join(tracebacks)

    def exceptions(self):
        """ Return a list of exceptions info, starting at """
            return [] +[1].exceptions()
        except AttributeError:
            return []

    def applicationDetails(self):
        """ Add MoinMoin details to system details """
        from MoinMoin import version
        return ['MoinMoin: Release %s (%s)' % (version.release,

    def formatExceptionMessage(self, info):
        """ Handle multiple paragraphs in exception message """
        text = cgitb.View.exceptionMessage(self, info)
        text = text.split('\n\n')
        text = ''.join([self.formatter.paragraph(item) for item in text])
        return text

def handle(request):
    """ Handle failures
    Display fancy error view, or fallback to simple text traceback
    savedError = sys.exc_info()
        debug = ('debug' in getattr(request, 'form', {}) or
                 'MOIN_DEBUG' in os.environ)
        handler = cgitb.Hook(file=request, viewClass=View, debug=debug)
        printTextException(request, savedError)
        request.write('\nAdditionally cgitb raised this exception:\n')

def printTextException(request, info=None):
    """ Simple text exception that should never fail
    Print all exceptions in a composite error.
    import traceback
    from MoinMoin import wikiutil
    if info is None:
        info = sys.exc_info()
        exceptions = [info] + info[1].exceptions()
    except AttributeError:
        exceptions = [info]
    for info in exceptions:
        text = ''.join(traceback.format_exception(*info))
        text = wikiutil.escape(text)