On Wednesday, 14 October 2020 15:43:05 UTC+1, hyan...@googlemail.com wrote: > > I'm creating my own bespoke 404 / 400 error page by editing routes.py like > this: > > routes_onerror = [('app/400', '/app/default/custom_400.html')] > > and I have 2 questions: > > 1) The custom_400 page is displayed when from within a controller, I call > e.g. > > raise HTTP(400, "my error message text") > > How do I access/display the error message text within my custom_400.html > page? > > 2) If it is is not an HTML view which is being called, but a JSON view, I > want to return a JSON page instead which, for example, reads {"error":"my > error message text"}. How do I do that? Can I just create a > custom_400.json view, and remove the .html from my line in routes.py? > > If I remember correctly, the message in your point 1) will only display if you do not have a custom page.... it overrides the default plain http error code text that web2py produces. As to point 2) which may help you solve point 1) as well, I think you may need to use dynamic error pages.
For example, I have a controller called 'errors' that looks like this: def error_404(): """ Dynamic 404 error page... receives <Storage {'ticket': 'None', 'code': '404', 'request_url': '/the_url', 'requested_uri': 'the_full_uri'}> """ # Set response code to original error, otherwise it will be 302. response.status = 404 is_property = '/propert' in request.vars.requested_uri.lower() if is_property: return response.render('errors/error_404_property.html', {}) # This is an ordinary view so pass variables in the dict as 2nd parameter. else: return response.render('errors/error_404_general.html', {}) Then I have two (normal) views in /applications/myapp/views/errors that correspond to the two views used above with different messages. In routes.py: routes_onerror = [ (r'*/400', r'/static/400.html'), # bad request (r'*/403', r'/static/403.html'), # forbidden (r'*/404', r'/errors/error_404'), # DYNAMIC - not found page ] Having said all that, to return json on 400 error, I would test whether this works for you: No views need to be created. In the errors controller, in the function you create, just return dict(error='my err message') In routes_onerror, try: ('app/400', '/app/default/custom_400.json'). Web2py's automatic json conversion should kick in and return the json if all goes well. Remember to restart your app each time you change anything in routes.py. See: http://www.web2py.com/books/default/chapter/29/03/overview?search=json#Simple-examples for how the .json extension should automatically work. HTH, -- 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 web2py+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/9ec7f519-02b8-4c39-8566-fc03334d0151o%40googlegroups.com.