On Wednesday, January 18, 2017 at 10:26:10 AM UTC-5, Carl Hunter Roach 
wrote:
>
> To routes.py I have:
>
> routes_onerror = [
> ('*/400', '/app/controller/error_handler')
> ]
>
> So for a raise HTTP(400, "Explanation Text")
>
> and from error_handler() I can get request.vars.code (400, in this 
> example).
>
> How can error_hander() get the text "Explanation Text" ?
>

Looking at the internal code, it doesn't look like you can get the "body" 
argument to HTTP() in the error handler. However, here are a couple of 
hacks:

request.wsgi.wsgi_environ['error_message'] = 'Explanation Text'
raise HTTP(400)

Then in the error handler:

def error_handler():
    error_message = request.env.error_message

That works because request.wsgi.wsgi_environ is the original WSGI 
environment dictionary for the request, and it gets re-used by the error 
handler (so it ends up in request.env in the error handler).

Alternatively, you can append your error message to request.url, which 
itself is passed to the error handler:

request.url = '%s&error_message=%s' % (request.url, 'Explanation Text')

And in the error handler:

def error_handler():
    error_message = request.vars.error_message

That is really just adding an additional variable to the query string that 
gets passed to the error handler by embedding the new variable within 
request.url.

Alternatively, if you have a limited number of messages, you can simply use 
a different numeric response code for each case to distinguish the errors. 
You don't have to limit yourself to the standard HTTP response codes, as in 
this case, the code is only used internally and never sent to the client 
(you can set a new response status when finally sending a response to the 
client).

Anthony 

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to