Generate ticket "manually"

72 views
Skip to first unread message

Brendan Barnwell

unread,
Dec 13, 2017, 5:18:40 PM12/13/17
to web2py-users
In some situations, when my web2py code raises an exception, what I want to do is log information about what happened, but then handle it in some way that doesn't involve the default error handling.  The advantage of the default error handling, though, is that it generates an error ticket.  Is there a way to "manually" generate a ticket, so that I could log the ticket ID for later inspection, while still attempting to recover from the error?  I'd like to be able to do something like this (inside a function in a controller or module):

try:
    do_something()
except ValueError as e:
    ticket = create_ticket_from_exception_somehow(e)
    my_logger.log(ticket.id_number)
    # code here to recover from error

Is there a function like create_ticket_from_exception_somehow?  All I can find in the docs is this:

"""

If an exception (other than HTTP) is raised, web2py does the following:

  • Stores the traceback in an error file and assigns a ticket number to it.
  • Rolls back all open database transactions.
  • Returns an error page reporting the ticket number.

"""


I want to do just the first part of that: store the traceback with the ticket number, so I can later view the ticket from the admin interface, but NOT do anything else --- just give me the ticket number and let me log it myself.

Anthony

unread,
Dec 14, 2017, 7:10:40 AM12/14/17
to web2py-users
Could be a little complicated. The relevant code is in gluon.restricted -- see https://github.com/web2py/web2py/blob/4f51647b2f5bd68887ddd9fe139c6bbc0fa885d4/gluon/restricted.py#L115.

You could create a RestrictedError (don't raise it, though, as that will trigger web2py's error processing). Calling the .log() method on that error object should then generate the ticket. Note, you'll be responsible for specifying the proper values for the "layer", "code", and "environment" arguments to RestrictedError() (for "environment", current.globalenv should be the proper value).

Anthony

Brendan Barnwell

unread,
Dec 14, 2017, 10:36:08 PM12/14/17
to web2py-users
On Thursday, December 14, 2017 at 4:10:40 AM UTC-8, Anthony wrote:
Could be a little complicated. The relevant code is in gluon.restricted -- see https://github.com/web2py/web2py/blob/4f51647b2f5bd68887ddd9fe139c6bbc0fa885d4/gluon/restricted.py#L115.


That is unfortunate.  Would be nice to have this in a more modularized fashion, but I'll see if I can make it work.
 
You could create a RestrictedError (don't raise it, though, as that will trigger web2py's error processing). Calling the .log() method on that error object should then generate the ticket. Note, you'll be responsible for specifying the proper values for the "layer", "code", and "environment" arguments to RestrictedError() (for "environment", current.globalenv should be the proper value).


Cool, thanks.  But what about "code"?  It looks like RestrictedError is mainly called from the restricted() function, and everywhere I can see that calls restricted() has a bunch of complicated logic in the same function to determine what to pass for its "ccode" argument.  Is there a way to access this information from within controller code?

Anthony

unread,
Dec 15, 2017, 12:22:44 PM12/15/17
to web2py-users
On Thursday, December 14, 2017 at 10:36:08 PM UTC-5, Brendan Barnwell wrote:
On Thursday, December 14, 2017 at 4:10:40 AM UTC-8, Anthony wrote:
Could be a little complicated. The relevant code is in gluon.restricted -- see https://github.com/web2py/web2py/blob/4f51647b2f5bd68887ddd9fe139c6bbc0fa885d4/gluon/restricted.py#L115.


That is unfortunate.  Would be nice to have this in a more modularized fashion, but I'll see if I can make it work.

Feel free to make a pull request. Note, depending on your needs, some other options might be:
  • Capture and store the tracebacks yourself. You could create a db.error table and then use code like:
        try:
           
[some code]
       
except:
            db
.error.insert(exception=traceback.format_exc())
  • Create a custom error handler via routes_onerror (see http://web2py.com/books/default/chapter/29/04/the-core#Routes-on-error).  That will allow you to handle errors however you like while still generating error tickets to be viewed in admin. Note, it will still do a database rollback before passing control to your error handler, and although there is no HTTP redirect, it will generate a new web2py environment and run the models again.
 
You could create a RestrictedError (don't raise it, though, as that will trigger web2py's error processing). Calling the .log() method on that error object should then generate the ticket. Note, you'll be responsible for specifying the proper values for the "layer", "code", and "environment" arguments to RestrictedError() (for "environment", current.globalenv should be the proper value).


Cool, thanks.  But what about "code"?  It looks like RestrictedError is mainly called from the restricted() function, and everywhere I can see that calls restricted() has a bunch of complicated logic in the same function to determine what to pass for its "ccode" argument.  Is there a way to access this information from within controller code?

I don't think so. For errors in models, the code would be the model file. For controllers, the code would be the controller file with an extra line added at the bottom to run the function that was called. And for views, the code would be the parsed view code. But you could pass in whatever code you think is relevant (or none at all). Either way, you'll still get the traceback.

Anthony 
Reply all
Reply to author
Forward
0 new messages