Can't get routes_onerror to working properly when mapping domain to apps

77 views
Skip to first unread message

Lisandro

unread,
Mar 3, 2016, 9:25:12 AM3/3/16
to web2py-users
I have several web2py apps, and each one is accessibly from a specific domain. I've achieved this using routes.py.
Also, I'm using routes_onerror inside routes.py in order to show a custom static html file on error. That is working ok.
This is my working routes.py:

# -*- coding: utf-8 -*-

domains
= {
   
'mainapp.com': 'mainapp',
   
'app1.com':    'app1',
   
'app2.com':    'app2',
   
'app3.com':    'app3'}

apps
= ['mainapp', 'app1', 'app2', 'app3']


routers
= dict(
  BASE
= dict(
    default_controller
= 'default',
    default_function
= 'index',
    domains
= domains,
    root_static
= ['robots.txt'],
    map_static
= True,
    exclusive_domain
= True,
 
)
)


routes_onerror
= []
for app in apps:
   
for code in ['403', '404', '500', '503']:
        routes_onerror
.append((r'%s/%s' %(app, code), r'/%s/static/%s.html' %(app, code)))
    routes_onerror
.append((r'%s/*' %app, r'/%s/static/500.html' %app))


Up to here, working ok.
Now, I would like to send an email when an internal error server happened, that is, error 500.
So I've modified the routes_onerror part to this:

routes_onerror = []
for app in apps:
   
for code in ['403', '404', '503']:
        routes_onerror
.append((r'%s/%s' %(app, code), r'/%s/static/%s.html' %(app, code)))
        routes_onerror
.append((r'%s/500' %app, '/mainapp/admin/error_handler'))
    routes_onerror
.append((r'%s/*' %app, '/mainapp/admin/error_handler'))

Basically, it says that 403, 404 and 503 errors will still return a static html file, but error 500 and other types of errors will be processed by /mainapp/admin/error_handler

This works perfectly if the error is thrown from mainapp (that is, the one that also handles the error).
But when an error occurs inside app1, app2 or app3, web2py shows the message "invalid function (default/mainapp)"

It appears to be that web2py is not calling correctly the /main/app/error_handler
Maybe the configuration of routers is some how messed up, and I'm doing it wrong.

Any help or clarification on this will be appreciated.
Regards,
Lisandro.




Lisandro

unread,
Mar 3, 2016, 9:33:40 AM3/3/16
to web2py-users
Well, sorry to bother, I think I got it.

I've just had to set the full path to the error handler, like this:

routes_onerror = []
for app in apps:
   
for code in ['403', '404', '503']:
        routes_onerror
.append((r'%s/%s' % (app, code), r'/%s/static/%s.html' % (app, code)))

        routes_onerror
.append((r'%s/500' % app, 'http://mainapp.com/admin/error_handler'))
    routes_onerror
.append((r'%s/*' % app, 'http://mainapp.com/admin/error_handler'))

Because mainapp.com is configured to serve the application mainapp, it's not necessary to include it in the url.
However, it's good to consider that, on error, the user will be redirected to other domain, I don't like the idea too much. But that's another story.

Anthony

unread,
Mar 3, 2016, 9:49:52 AM3/3/16
to web2py-users

routes_onerror = []
for app in apps:
   
for code in ['403', '404', '503']:
        routes_onerror
.append((r'%s/%s' % (app, code), r'/%s/static/%s.html' % (app, code)))
        routes_onerror
.append((r'%s/500' % app, 'http://mainapp.com/admin/error_handler'))

Also, you should move that last line outside of the for loop.

Anthony

Lisandro

unread,
Mar 3, 2016, 9:55:17 AM3/3/16
to web2py-users
Yes in deed! Thanks for the heads up!
Reply all
Reply to author
Forward
0 new messages