Hello,

I've done some research in this group and the CherryPy group and came
up with a way to point CherryPy to custom 404 and 500 error pages.
The discussions I found were fairly old and some of the links to
suggestions no longer work, so I wanted to put this out there and make
sure I wasn't missing anything.

In CherryPy-2.2.1-py2.4.egg\cherrypy\_cperror.py I adjusted and added
a few lines of code to change these classes:

class HTTPError(Error):
    """ Exception used to return an HTTP error code to the client.
        This exception will automatically set the response status and
body.

        A custom message (a long description to display in the
browser)
        can be provided in place of the default.
    """

    def __init__(self, status=500, message=None):
        self.status = status = int(status)
        if status < 400 or status > 599:
            raise ValueError("status must be between 400 and 599.")
        self.message = message
        Error.__init__(self, status, message)
        """ used to gently pass the user to an error page comment out
for debugging """
        """ raise HTTPRedirect('/error') """
        if status == 400:
            raise HTTPRedirect('/notfound')
        elif status == 500:
            raise HTTPRedirect('/error')


    def set_response(self):
        import cherrypy
        handler =
cherrypy._cputil.get_special_attribute("_cp_on_http_error",
"_cpOnHTTPError")
        handler(self.status, self.message)


class NotFound(HTTPError):
    """ Happens when a URL couldn't be mapped to any class.method """

    def __init__(self, path=None):
        if path is None:
            import cherrypy
            path = cherrypy.request.path
        self.args = (path,)
        HTTPError.__init__(self, 404, "The path  %s was not found." %
repr(path))
        """ used to gently pass the user to a not found page comment
out for debuggin """
        raise HTTPRedirect('/notfound')


Is this the simplest way to do it?  Being new to bothPython, TG and
CherryPy I wasn't sure if there was a method within TG to do this and
not have to worry about possibly breaking CherryPy (or having to
maintain CherryPy code).

I suppose the way I did it in CherryPy is much the way you would
define custom pages in Apache or even IIS... with these redirects
sitting at the web server level, not the code level.

Any thoughts?  Do you think this is a stable way to manage custom
error pages?

Thanks!

-Steve


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to