How to redirect an error ticket response to the current url

360 views
Skip to first unread message

aleon...@gmail.com

unread,
Apr 29, 2014, 2:07:03 AM4/29/14
to web...@googlegroups.com
Hi.

Im trying to display the error ticket caused by a controller "without changing the current url" on my browser, also in the same browser tab of course.

Im using Live Reload to display the changes to my pages and its quite annoying to manually refreshing the browser each time i have an error on the controller.

This is my routers.py at web2py folder:

routers = dict(
    BASE
=dict(
        default_application
='test',
   
),
    test
=dict(
        default_controller
= 'index',
        default_function
= 'index'
   
),
)
error_message_ticket
='''
  <html><head>
    <script language="javascript">
      location.replace("/admin/default/ticket/%(ticket)s")
    </script>
    <!-- this is junk text else IE does not display the page: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx //-->
  </head></html>'''


I grabed the error_message_ticket from Stack Overflow which allows me to display the ticket at the same browser tab but its not quite what im looking for, the url still changes.

Thanks in advance.

aleon...@gmail.com

unread,
May 1, 2014, 2:51:47 AM5/1/14
to web...@googlegroups.com
Im gonna try to explain it better.

I have an application with a controller that has a function that produces an error. ex: myapp/default/index
on routes.py im redirecting the errors to otherapp/default/display_errors
to watch the ticket i have to go to "http://localhost:8000/admin/default/ticket/myapp/127.0.0.1.2014-05-01.07-54-28.fad8beec-c539-4243-b7bb-116ce76cc39c"
i would like  otherapp/default/display_errors to return the same response as that url

Ive tried many things like, none of them are working very well.
def display_errors():
    import urllib
    resp = urllib.openurl("http://localhost:8000/admin/default/ticket/myapp/127.0.0.1.2014-05-01.07-54-28.fad8beec-c539-4243-b7bb-116ce76cc39c"")
    return resp.read()

it returns the admin login page, does not work.

app = request.application
    myversion
= request.env.web2py_version
    ticket
= request.vars.ticket.split("/")[1]
   
from gluon.restricted import RestrictedError
    e
= RestrictedError()
    e
.load(request, app, ticket)

   
return response.render("default/ticket.html", dict(app=app,
                ticket
=ticket,
                output
=e.output,
                traceback
=(e.traceback,), # and TRACEBACK(e.traceback)),
                snapshot
=e.snapshot,
                code
=e.code,
                layer
=e.layer,
                myversion
=myversion))




Alfonso Serra

unread,
May 1, 2014, 3:07:16 AM5/1/14
to web...@googlegroups.com
Im sorry posted by accident without finishing.
the last code works but with some limitations.
Im trying to replicate the function ticket from admin/default.py into the display_errors function

It displays the error on the url that caused the ticket but it is not using the same layout as the admin app, so some things doesnt appear correctly.
i was hoping there were an easier way, maybe with exec_environment or using XMLRPC but i have to learn how to use them yet.



--
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/DpHra_ypD4g/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.

aleon...@gmail.com

unread,
May 6, 2014, 5:39:10 PM5/6/14
to web...@googlegroups.com
Ok i finally got it.
If you want to bypass the ticket system and display the errors directly on the page you are working on, follow these steps:

1.-Install mechanize which it is a library that automates webforms.

2- Redirect the errors to a custom controller in any application:
Edit routes.py and add:
routes_onerror = [(r'*/*', r'/myapp/index/errors')]
so myapp has a controller with a function called "errors" which will handle the error request

Edit myapp/controllers/index.py and add the errors function.
def errors():
    ticket
= request.vars.ticket
    url
= "http://127.0.0.1:8000/admin/default/ticket/" + ticket

   
import mechanize
    br
= mechanize.Browser()
    br
.open(url)
    br
.select_form(nr=0)
   
#administrator password to enter the admin area
    br
['password'] = "1234"
    res
= br.submit()
    content
= res.read()
   
return content

Finally reload routes by clicking reload routes at admin area or restarting the rocket server.

And thats it. You will be able to see the tickets at the same page you are working on without having to navigate to the admin area or refresh your page if you use LiveReload.

Note that this trick should be only used in development.
Let me know if it helps or if you have any doubts.

Thanks.


Reply all
Reply to author
Forward
0 new messages