Dumb Question/Bug

47 views
Skip to first unread message

Mark Billion

unread,
Dec 23, 2014, 1:27:38 PM12/23/14
to web...@googlegroups.com
Works Just Fine

def cli_add():
    try:
        db.client.au_usr.default = auth.user_id
    except:
        redirect(URL('index'))
    form = SQLFORM(db.client, fields = ['d_fn', 'd_ln', 'd_aka', 'd_ss'], labels = {'d_ss': 'Social Security', 'd_aka': 'Any Aliases', 'd_65': 'Older than 65'}).process(next=URL('clients'))    
    return dict(form=form)

BUT WHEN I MOVE THE FORM CALL INTO THE TRY LOOP:

def cli_add():
    try:
        db.client.au_usr.default = auth.user_id
        form = SQLFORM(db.client, fields = ['d_fn', 'd_ln', 'd_aka', 'd_ss'], labels = {'d_ss': 'Social Security', 'd_aka': 'Any Aliases', 'd_65': 'Older than 65'}).process(next=URL('clients'))    
    except:
        redirect(URL('index'))

    return dict(form=form)

The look fails into the except redirect.....

Any thoughts?

Leonel Câmara

unread,
Dec 23, 2014, 1:31:21 PM12/23/14
to web...@googlegroups.com
Why are you using an except without specifying the exception? Anyway just do this:

@auth.requires_login()
def cli_add():
    db.client.au_usr.default = auth.user_id
    form = SQLFORM(db.client, fields = ['d_fn', 'd_ln', 'd_aka', 'd_ss'], labels = {'d_ss': 'Social Security', 'd_aka': 'Any Aliases', 'd_65': 'Older than 65'}).process(next=URL('clients'))    
    return dict(form=form)

Mark Billion

unread,
Dec 23, 2014, 1:34:45 PM12/23/14
to web...@googlegroups.com
Why are you using an except without specifying the exception? Trying to catch all possible exceptions.  

Niphlod

unread,
Dec 23, 2014, 2:44:57 PM12/23/14
to web...@googlegroups.com
are you aware that in web2py any HTTP is a subclass of exception ? Never, never, never use an exception without specifying what exception your code may raise.

Anthony

unread,
Dec 23, 2014, 2:54:11 PM12/23/14
to web...@googlegroups.com
More specifically, when you provide the "next" argument to SQLFORM.process(), it ultimately calls redirect(), which raises an HTTP exception. If you put the .process() call inside a try/except, that HTTP exception will get caught, and in your case, you are then ignoring it. We should probably spell this out more clearly in the book.

You could instead do something like:

    except Exception as e:
       
if isinstance(e, HTTP):
           
raise e # re-raise the exception in case of a redirect
        redirect
(URL('index'))

But generally it is much better to catch specific exceptions.

Anthony

Mark Billion

unread,
Dec 23, 2014, 3:29:02 PM12/23/14
to web...@googlegroups.com
Thanks guys -- that makes a ton of sense.  I really appreciate it.  

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



--
Mark M. Billion
1904 N. Lincoln
Wilmington, DE 19806
302.416.2199

Mark Billion

unread,
Dec 26, 2014, 9:15:40 AM12/26/14
to web...@googlegroups.com
Just two questions for my edification:
1. Why did HTTP get grouped into exceptions
2. Is there a security reason not to catch all non-http exceptions, ie except EXCEPTION

Anthony

unread,
Dec 26, 2014, 10:13:41 AM12/26/14
to web...@googlegroups.com
On Friday, December 26, 2014 9:15:40 AM UTC-5, Mark Billion wrote:
Just two questions for my edification:
1. Why did HTTP get grouped into exceptions

By making HTTP an exception, this allows both the framework and app code to short-circuit a given response at any point and immediately return something (e.g., you can raise an HTTP exception in a model, and it will return without executing the controller or view).
 
2. Is there a security reason not to catch all non-http exceptions, ie except EXCEPTION

I don't think it's necessarily a security issue. It's just that you usually don't want to catch all exceptions, particularly if your code isn't doing anything to actually inspect and handle the exception. You could end up introducing silent failures and subtle bugs. It could be OK to catch all exceptions if your "except" block then does something with the exception, such as executing special error handling code or re-raising particular exceptions.

Anthony

Mark Billion

unread,
Dec 26, 2014, 10:41:19 AM12/26/14
to web...@googlegroups.com
Awesome.  I really appreciate the help.

--
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/hWvrDzyR49M/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.
Reply all
Reply to author
Forward
0 new messages