how to access the message parameter of a raise HTTP?

188 views
Skip to first unread message

Carl Hunter Roach

unread,
Jan 18, 2017, 10:26:10 AM1/18/17
to web2py-users
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" ?

Anthony

unread,
Jan 18, 2017, 6:18:53 PM1/18/17
to web2py-users

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

Carl Hunter Roach

unread,
Jan 19, 2017, 4:32:51 AM1/19/17
to web2py-users
thanks.

I don't like adding UI Text to URLs as it's too easy for 3rd-parties to generate URls and have your site serve anything they like :)

Your suggestion of ditching the standard HTTP status codes spurs a thought: I could drop Web2py's HTTP altogether and just redirect to my own error handler page. That way HTTP remains untouched and it's clearer that I've written an alternative implementation.

Has anyone else come across this issue with Web2py's HTTP missing body text?

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/Mg5kK2AuoMQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
----
Carl D Hunter Roach

Anthony

unread,
Jan 19, 2017, 6:39:38 AM1/19/17
to web2py-users
On Thursday, January 19, 2017 at 4:32:51 AM UTC-5, Carl Hunter Roach wrote:
thanks.

I don't like adding UI Text to URLs as it's too easy for 3rd-parties to generate URls and have your site serve anything they like :)

Note, this is an internal redirect, so the end user never sees the query string passed to the error handler -- they continue to see the original URL in their browser address bar (i.e., the URL of the page that triggered the error).
 
Your suggestion of ditching the standard HTTP status codes spurs a thought: I could drop Web2py's HTTP altogether and just redirect to my own error handler page. That way HTTP remains untouched and it's clearer that I've written an alternative implementation.

Good point -- since you are triggering the error response yourself, you do have more control. However, I wouldn't necessarily do a standard 303 redirect, as that (a) wastes a round trip to the server, and (b) does not return the proper HTTP response code with the initial response. Instead, though, you can create an error handling function in a model file or module and call it from wherever needed. In that handler, you can set response.status to the appropriate HTTP status code, which will get sent to the browser (along with whatever response is generated by the handler).

Anthony

Carl Hunter Roach

unread,
Jan 19, 2017, 6:48:56 AM1/19/17
to web2py-users
good points!

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

would keep it clean and provide a function to document the reason why this approach is implemented.
You've sold me!
thanks


--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/Mg5kK2AuoMQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages