[mod_python] Fancy Shmancy Exception Output

Chris Hagner CHagner at fool.com
Tue Apr 24 09:41:38 EST 2001


Yeah, the colors are pretty nasty... I'm thinking Yee (the author) is
color-blind...

FYI, cgitb does not have any 2.1 dependencies... I have it wired into my
handler and it works great (running 2.0).  As I said, this is less ideal,
since it should really be wired into apache.py so that the handler isn't
bothered with catching EVERY error...

I've gotten some pretty good feedback from our developers.  We're so used to
getting an error and then having to go to our tail of the error log (and
then to the code to see what line it is)... it's very nice to shorten that
cycle.

Here's the code snippet I used to integrate it (Prepare to be wowed...)

import cgitb

# Within the handler module's handler method...

    try:
        # all of the stuff that happens in my handler's primary method
    except:
       # NOTE: I use my own request object to wrap the apache-provided one, 
       # so these calls are not the standard apache API calls
       req.setContentType("text/html")
       req.sendHTTPHeader()
       # Calling the handle method of cgitb just prints the output of 
       # breaker() and html() to standard out.
       # Thus, I skip that and do it myself.
       errHtml = str(cgitb.breaker()) + str(cgitb.html())
       req.write(errHtml)

    return apache.OK

Grisha, I'm more than happy to make the mod to apache.py if you don't have
the time... All I'd need is a new directive that is accessible from within
mod_python.apahce.ReportError().  I don't think it would require any other
changes.  

Okay, thinking about it more, I'd add a parameter to ReportError called
error_handler (just like how the debug parameter is passed in)...

Thus, something like this would be added to mod_python.apache.Dispatch()

        config = _req.get_config()
        if config.has_key("PythonErrorHandler"):
            error_handler = config["PythonErrorHandler"]
        else:
            error_handler = None

and all ReportError() calls would have error_handler passed in...

        result = ReportError(etype, value, traceback, htype=htype,
hname=handler, debug=debug, error_handler=error_handler)

Then, ReportError would have something like this...

         if not debug:
                return HTTP_INTERNAL_SERVER_ERROR
         else:
                # write to client
                if error_handler:
                        req.content_type = 'text/html'
                        s = str(cgitb.breaker()) + str(cgitb.html())
                else:
                        req.content_type = 'text/plain'
                        s = '\nMod_python error: "%s %s"\n\n' % (htype,
hname)
                        for e in traceback.format_exception(etype, evalue,
etb):
                             s = s + e + '\n'

                req.send_http_header()
                req.write(s)

                return DONE

This code is a bit of a brain dump on my part, so don't even think that I've
run/tested it yet... :-)  Let me know if I'm missing something... 

Chris

-----Original Message-----
From: Gregory (Grisha) Trubetskoy [mailto:grisha at modpython.org]
Sent: Monday, April 23, 2001 11:54 PM
To: Chris Hagner
Cc: mod_python at modpython.org
Subject: Re: [mod_python] Fancy Shmancy Exception Output



I propose a new directive:

PythonPinkError On

seriosly speaking - this looks neat. I wonder if it has anything specific
to python 2.1 in it, since mod_python has to be compatible with 1.5.2 I
think, at least for a while...

On Mon, 23 Apr 2001, Chris Hagner wrote:

> Hey gang,
>
> There's a utility called pydoc that's in the new python 2.1 release...
> anyway, the author of it wrote a really snazzy error handling page for cgi
> scripts (called cgitb)... check out the output and see if you find it
> useful.
>
> Here's a sample cgitb output page...
> http://www.lfw.org/python/test4.html
>
> Here's where you can get the module for it...
> http://www.lfw.org/python/
>
> Okay, except for the nasty colors, I'd love to see something closer to
this
> in the standard mod_python error handler(apache.py).  I guess the most
> flexible way would be to have a PythonErrorModule directive and allow each
> person to override the default error display with their own module (in
this
> case, the cgitb module).  The key is that this functionality would only
> really be appropriate for a development environment...
>
> Thoughts?
>
> Chris
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python
>



More information about the Mod_python mailing list