예제 #1
0
def format_eval_html(exc_data, base_path, counter):
    short_formatter = EvalHTMLFormatter(
        base_path=base_path,
        counter=counter,
        include_reusable=False)
    short_er, extra_data = short_formatter.format_collected_data(exc_data)
    short_text_er, text_extra_data = formatter.format_text(exc_data, show_extra_data=False)
    long_formatter = EvalHTMLFormatter(
        base_path=base_path,
        counter=counter,
        show_hidden_frames=True,
        show_extra_data=False,
        include_reusable=False)
    long_er, extra_data_none = long_formatter.format_collected_data(exc_data)
    long_text_er = formatter.format_text(exc_data, show_hidden_frames=True,
                                         show_extra_data=False)[0]
    long_xml_er = formatter.format_xml(exc_data, show_hidden_frames=True, 
                                  show_extra_data=False)[0]
    short_xml_er = formatter.format_xml(exc_data, show_hidden_frames=False, 
                                  show_extra_data=False)[0]
    
    if short_formatter.filter_frames(exc_data.frames) != \
        long_formatter.filter_frames(exc_data.frames):
        # Only display the full traceback when it differs from the
        # short version
        long_text_er = cgi.escape(long_text_er)
        full_traceback_html = """
        <div id="full_traceback" class="hidden-data">
        %s
        </div>
        <div id="long_text_version" class="hidden-data">
        <textarea style="width: 100%%" rows=%s cols=60>%s</textarea>
        </div>
        """ % (long_er, len(long_text_er.splitlines()), long_text_er)
    else:
        full_traceback_html = ''

    short_text_er = cgi.escape(short_text_er)
    
    long_xml_leng = len(long_xml_er.splitlines())
    if long_xml_leng > 50:
        long_xml_leng = 50

    short_xml_leng = len(short_xml_er.splitlines())
    if short_xml_leng > 50:
        short_xml_leng = 50

    return """
    <div id="short_traceback">
    %s
    </div>
    <div id="short_text_version" class="hidden-data">
    <textarea style="width: 100%%" rows=%s cols=60>%s</textarea>
    </div>
    <div id="long_xml_version" class="hidden-data">
    <textarea style="width: 100%%" rows=%s cols=60>%s</textarea>
    </div>
    <div id="short_xml_version" class="hidden-data">
    <textarea style="width: 100%%" rows=%s cols=60>%s</textarea>
    </div>
    %s
    """ % (short_er, len(short_text_er.splitlines()), short_text_er,
           long_xml_leng, cgi.escape(long_xml_er), 
           short_xml_leng, cgi.escape(short_xml_er), 
           full_traceback_html), extra_data
def handle_exception(
    exc_info,
    error_stream,
    html=True,
    debug_mode=False,
    error_email=None,
    error_log=None,
    show_exceptions_in_wsgi_errors=False,
    error_email_from="errors@localhost",
    smtp_server="localhost",
    error_subject_prefix="",
    error_message=None,
    simple_html_error=False,
):
    """
    For exception handling outside of a web context

    Use like::

        import sys
        import paste
        import paste.error_middleware
        try:
            do stuff
        except:
            paste.error_middleware.exception_handler(
                sys.exc_info(), paste.CONFIG, sys.stderr, html=False)

    If you want to report, but not fully catch the exception, call
    ``raise`` after ``exception_handler``, which (when given no argument)
    will reraise the exception.
    """
    reported = False
    exc_data = collector.collect_exception(*exc_info)
    extra_data = ""
    if error_email:
        rep = reporter.EmailReporter(
            to_addresses=error_email,
            from_address=error_email_from,
            smtp_server=smtp_server,
            subject_prefix=error_subject_prefix,
        )
        rep_err = send_report(rep, exc_data, html=html)
        if rep_err:
            extra_data += rep_err
        else:
            reported = True
    if error_log:
        rep = reporter.LogReporter(filename=error_log)
        rep_err = send_report(rep, exc_data, html=html)
        if rep_err:
            extra_data += rep_err
        else:
            reported = True
    if show_exceptions_in_wsgi_errors:
        rep = reporter.FileReporter(file=error_stream)
        rep_err = send_report(rep, exc_data, html=html)
        if rep_err:
            extra_data += rep_err
        else:
            reported = True
    else:
        error_stream.write("Error - %s: %s\n" % (exc_data.exception_type, exc_data.exception_value))
    if html:
        if debug_mode and simple_html_error:
            return_error = formatter.format_html(
                exc_data, include_hidden_frames=False, include_reusable=False, show_extra_data=False
            )
            reported = True
        elif debug_mode and not simple_html_error:
            error_html = formatter.format_html(exc_data, include_hidden_frames=True, include_reusable=False)
            head_html = ""
            return_error = error_template(head_html, error_html, extra_data)
            extra_data = ""
            reported = True
        else:
            msg = (
                error_message
                or """
            An error occurred.  See the error logs for more information.
            (Turn debug on to display exception reports here)
            """
            )
            return_error = error_template("", msg, "")
    else:
        return_error = None
    if not reported and error_stream:
        err_report = formatter.format_text(exc_data, show_hidden_frames=True)[0]
        err_report += "\n" + "-" * 60 + "\n"
        error_stream.write(err_report)
    if extra_data:
        error_stream.write(extra_data)
    return return_error
def handle_exception(
    exc_info,
    error_stream,
    html=True,
    debug_mode=False,
    error_email=None,
    error_log=None,
    show_exceptions_in_wsgi_errors=False,
    error_email_from='errors@localhost',
    smtp_server='localhost',
    error_subject_prefix='',
    error_message=None,
    simple_html_error=False,
):
    """
    For exception handling outside of a web context

    Use like::

        import sys
        import paste
        import paste.error_middleware
        try:
            do stuff
        except:
            paste.error_middleware.exception_handler(
                sys.exc_info(), paste.CONFIG, sys.stderr, html=False)

    If you want to report, but not fully catch the exception, call
    ``raise`` after ``exception_handler``, which (when given no argument)
    will reraise the exception.
    """
    reported = False
    exc_data = collector.collect_exception(*exc_info)
    extra_data = ''
    if error_email:
        rep = reporter.EmailReporter(to_addresses=error_email,
                                     from_address=error_email_from,
                                     smtp_server=smtp_server,
                                     subject_prefix=error_subject_prefix)
        rep_err = send_report(rep, exc_data, html=html)
        if rep_err:
            extra_data += rep_err
        else:
            reported = True
    if error_log:
        rep = reporter.LogReporter(filename=error_log)
        rep_err = send_report(rep, exc_data, html=html)
        if rep_err:
            extra_data += rep_err
        else:
            reported = True
    if show_exceptions_in_wsgi_errors:
        rep = reporter.FileReporter(file=error_stream)
        rep_err = send_report(rep, exc_data, html=html)
        if rep_err:
            extra_data += rep_err
        else:
            reported = True
    else:
        error_stream.write('Error - %s: %s\n' %
                           (exc_data.exception_type, exc_data.exception_value))
    if html:
        if debug_mode and simple_html_error:
            return_error = formatter.format_html(exc_data,
                                                 include_hidden_frames=False,
                                                 include_reusable=False,
                                                 show_extra_data=False)
            reported = True
        elif debug_mode and not simple_html_error:
            error_html = formatter.format_html(exc_data,
                                               include_hidden_frames=True,
                                               include_reusable=False)
            head_html = ''
            return_error = error_template(head_html, error_html, extra_data)
            extra_data = ''
            reported = True
        else:
            msg = error_message or '''
            An error occurred.  See the error logs for more information.
            (Turn debug on to display exception reports here)
            '''
            return_error = error_template('', msg, '')
    else:
        return_error = None
    if not reported and error_stream:
        err_report = formatter.format_text(exc_data,
                                           show_hidden_frames=True)[0]
        err_report += '\n' + '-' * 60 + '\n'
        error_stream.write(err_report)
    if extra_data:
        error_stream.write(extra_data)
    return return_error
예제 #4
0
 def format_text(self, exc_data, **kw):
     return formatter.format_text(exc_data, **kw)
예제 #5
0
 def format_text(self, exc_data, **kw):
     return formatter.format_text(exc_data, **kw)