routes_onerror doesn't redirect to a custom controller when an "invalid request" error is produced

168 views
Skip to first unread message

Carlos Fillol Sendra

unread,
May 6, 2014, 10:20:53 AM5/6/14
to web...@googlegroups.com

Imagine that I have an application called 'myapp' running in my local web2py server, and I include this in routes.py to manage HTTP errors using a custom controller (/myapp/portal/error_page):

routes_onerror = [
   
('myapp/*', '/myapp/portal/error_page'),
]


Then, when somebody access to myapp and a HTTP exception is raised, web2py framework catches it and redirects execution to my custom controller. Perfect! That is what I was looking for! :)

This works great most of the time, but I have observed that execution doesn't go through my custom controller when an "invalid request" error is produced (HTTP 400 error code). This happens for example, when I use a controller or a function name with special chars like '-'. Ex: http://127.0.0.1:8000/myapp/not-existing-controller-with-especial-chars

Debugging function regex_url_in in gluon/rewrite.py I have realised that request.application is None when an "invalid request" error is produced. Then  try_redirect_on_error doesn't know which is the application that produces the problem and is not able to redirect execution to the custom controller configured in routes.py.

I wonder if it would have sense to infer request.application from an invalid url to allow custom controller redirection when an invalid request error is produced.

# Maybe adding something like this in gluon.rewrite.regex_url_in?
# ...
match
= regex_url.match(path)
   
if not match:

       
# Added
        application_match
= re.search('^/(?P<a>\w+)/.*$', path)
        request
.application = application_match and application_match.group('a') or None

        invalid_url
(routes)
    request
.raw_args = (match.group('s') or '')
   
if request.raw_args.startswith('/'):
        request
.raw_args = request.raw_args[1:]
# ...

Thank you very much for web2py :)

Anthony

unread,
May 6, 2014, 1:22:26 PM5/6/14
to web...@googlegroups.com
+1

ermolaev...@gmail.com

unread,
Jul 19, 2015, 11:10:30 AM7/19/15
to web...@googlegroups.com
++11

how regirect if function or controller not found??

invalid function
invalid controller

routes.py in app_folder not worked ((

routes_onerror = [
    ('*/404', 'my_app/deal/index'),
...

not worked (

Reply all
Reply to author
Forward
0 new messages