Project

General

Profile

« Previous | Next » 

Revision 2076

sql.py: Added with_parsed_errors() and use it in try_insert()

View differences:

lib/sql.py
532 532

  
533 533
##### Heuristic queries
534 534

  
535
def try_insert(db, table, row, returning=None):
536
    '''Recovers from errors'''
537
    try: return insert(db, table, row, returning, recover=True)
535
def with_parsed_errors(db, func):
536
    '''Translates known DB errors to typed exceptions'''
537
    try: return func()
538 538
    except Exception, e:
539 539
        msg = str(e)
540 540
        match = re.search(r'duplicate key value violates unique constraint '
......
549 549
        if match: raise NullValueException([match.group(1)], e)
550 550
        raise # no specific exception raised
551 551

  
552
def try_insert(db, table, row, returning=None):
553
    '''Recovers from errors'''
554
    return with_parsed_errors(db, lambda: insert(db, table, row, returning,
555
        recover=True))
556

  
552 557
def put(db, table, row, pkey, row_ct_ref=None):
553 558
    '''Recovers from errors.
554 559
    Only works under PostgreSQL (uses `INSERT ... RETURNING`)'''

Also available in: Unified diff