List Info

Thread: Redirecting 404 requests to a handler in CherryPy 3.0.2




Redirecting 404 requests to a handler in CherryPy 3.0.2
user name
2007-12-16 15:50:46
Greetings,

I'm in the process of upgrading to CherryPy 3 from 2.2. One
of the
things that seems to have disappeared in the new version is
the
_cp_on_http_error method for handling custom errors. I used
to be able
to do this:

def _cp_on_http_error(self, status, message):
        cherrypy.response.body = [self.customError()] #call
an
internal method that would render out some html.

I have diligently read the manual, tried writing a custom
tool on the
'after_error_response' hook,  read the code and googled my
heart out,
but I have no idea how to do this in 3.

Any help?

-c

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Redirecting 404 requests to a handler in CherryPy 3.0.2
user name
2007-12-17 07:50:17
On 12/16/07, Cory <corbinrsmithgmail.com> wrote:
> I'm in the process of upgrading to CherryPy 3 from 2.2.
One of the
> things that seems to have disappeared in the new
version is the
> _cp_on_http_error method for handling custom errors. I
used to be able
> to do this:
>
> def _cp_on_http_error(self, status, message):
>         cherrypy.response.body = [self.customError()]
#call an
> internal method that would render out some html.
>
> I have diligently read the manual, tried writing a
custom tool on the
> 'after_error_response' hook,  read the code and googled
my heart out,
> but I have no idea how to do this in 3.
>
> Any help?

Try setting request.error_page in your config.  Here is the
docstring
for that attribute:
"""
    A dict of {error code: response filename} pairs. The
named response
    files should be Python string-formatting templates, and
can expect by
    default to receive the format values with the mapping
keys 'status',
    'message', 'traceback', and 'version'. The set of format
mappings
    can be extended by overriding HTTPError.set_response.
"""

HTH,

Christian
http://www.dowski.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Redirecting 404 requests to a handler in CherryPy 3.0.2
user name
2007-12-17 10:39:09
Thanks, but that isn't quite what I'm looking to do...

I have actual business logic that makes suggestions for different pages, so a static template with a few replacement variables isn't going to quite carry it.

Though, I suppose I could monkey-patch NotFound.set_response, but I'm not thrilled about it.

I did finally get something that worked, but it is always doing a check on before_finalize, which seems a tad wasteful.

def not_found_redirect(path):
   ; if cherrypy.response.status == 404 and cherrypy.request.path_info != path:
&nbsp; &nbsp; &nbsp; &nbsp;  raise cherrypy.InternalRedirect(path)

cherrypy.tools.not_found_redirect = cherrypy.tools.Tool ('before_finalize&#39;, not_found_redirect)

-c

On Dec 17, 2007 5:50 AM, Christian Wyglendowski < christiandowski.com">christiandowski.com> wrote:

On 12/16/07, Cory < corbinrsmithgmail.com">corbinrsmithgmail.com> wrote:
>; I'm in the process of upgrading to CherryPy 3 from 2.2. One of the
> things that seems to have disappeared in the new version is the
> _cp_on_http_error method for handling custom errors. I used to be able
> to do this:
>
> def _cp_on_http_error(self, status, message):
>         cherrypy.response.body = [self.customError()] #call an
> internal method that would render out some html.
>
> I have diligently read the manual, tried writing a custom tool on the
> 'after_error_response' hook,  read the code and googled my heart out,
>; but I have no idea how to do this in 3.
>
&gt; Any help?

Try setting request.error_page in your config.  Here is the docstring
for that attribute:
"&quot;"
   A dict of {error code: response filename} pairs. The named response
   files should be Python string-formatting templates, and can expect by
   default to receive the format values with the mapping keys 'status',
   ';message9;, 'traceback', and 'version'. The set of format mappings
   can be extended by overriding HTTPError.set_response.
"&quot;"

HTH,

Christian
http://www.dowski.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users&quot; group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )