IntegrityError is defined by the database driver, so needs to be imported. 
Actually, I forgot, we made this easier, so it is not adapter-dependent -- 
you can do:

try:
    ...
except db._adapter.driver.IntegrityError:
    ...

Another option is:

def insert_error_handler(table, fields, error):
    [handle the error]

db.mytable._on_insert_error = insert_error_handler

There is also an _on_update_error callback.

Anthony

On Friday, January 10, 2014 3:53:17 AM UTC-5, software.ted wrote:
>
> Tried the example you gave me:
>
> Try: 
>     ....
> Except IntegrityError: 
>     ....
>
>
> I am getting unresolved reference 'IntegrityError'
>
> Kind regards,
>
>
> On Thu, Jan 9, 2014 at 6:01 PM, Teddy Nyambe <softwa...@gmail.com<javascript:>
> > wrote:
>
>> Thanx Anthony,
>>
>> I will use [try]
>> On 9 Jan 2014 15:52, "Anthony" <abas...@gmail.com <javascript:>> wrote:
>>
>>> try:
>>>     db.test.insert(...)
>>> except IntegrityError:
>>>     [return friendly error message]
>>>
>>> Of course, if the inserts are done via form submission, you should 
>>> instead add an IS_NOT_IN_DB validator, in which case, it will automatically 
>>> check for duplicates and return the appropriate error message. Even if not 
>>> using a form, you can make use of the validator by using the 
>>> .validate_and_insert() method.
>>>
>>> Anthony
>>>
>>> On Thursday, January 9, 2014 2:31:00 AM UTC-5, software.ted wrote:
>>>>
>>>> Hi, 
>>>>
>>>> I am trying to find out the best way to catch errors generated by 
>>>> web2py especially those genereted by say DAL. Say forinstance if I 
>>>> have a table: 
>>>>
>>>> db.define_table('test', Field('xyz', 'integer', unique=True) 
>>>>
>>>> If i insert a dublicate field will get the exception and ticket: 
>>>>
>>>> IntegrityError: (1062, u"Duplicate entry 'X' for key 'xyz'") 
>>>>
>>>> Now i want to catch such an error and send a better message to a user, 
>>>> not the ticket etc. 
>>>>
>>>> Any ideas? 
>>>>
>>>> -- 
>>>> .......................................................................................
>>>>  
>>>>
>>>> Teddy Lubasi Nyambe 
>>>> Opensource Zambia 
>>>> Lusaka, ZAMBIA 
>>>>
>>>> Cell: +260 97 7760473 
>>>> website: http://www.opensource.org.zm 
>>>>
>>>> ~/ 
>>>> Human Knowledge belongs to the world! - AntiTrust 
>>>>
>>>> Man is a tool-using animal. Without tools he is nothing, with tools he 
>>>> is all - Thomas Carlyle 1795-1881 
>>>>
>>>> /~ 
>>>>
>>>  -- 
>>> 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 the Google 
>>> Groups "web2py-users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to web2py+un...@googlegroups.com <javascript:>.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>
>
> -- 
>
> .......................................................................................
> Teddy Lubasi Nyambe
> Opensource Zambia
> Lusaka, ZAMBIA
>
> Cell: +260 97 7760473
> website: http://www.opensource.org.zm
>
> ~/
> Human Knowledge belongs to the world! - AntiTrust
>
> Man is a tool-using animal. Without tools he is nothing, with tools he is 
> all - Thomas Carlyle 1795-1881
>
> /~ 
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to