Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1077 (closed defect: fixed)

Opened 13 years ago

Last modified 12 years ago

trapping exceptions raised inside SA's run_with_transaction

Reported by: skpatel20@… Owned by: anonymous
Priority: high Milestone: 1.0
Component: TurboGears Version:
Severity: critical Keywords: sqlalchemy exception trapping


Using sqlalchemy, it's not being possible to trap database errors while commitng. Example code:

# model.py

from sqlalchemy import * from sqlalchemy.ext.assignmapper import assign_mapper from turbogears.database import metadata, session

person_table = Table('person', metadata,

Column('id', Integer, primary_key=True), Column('name', String(30), unique=True, nullable=False)


class Person(object):


assign_mapper(session.context, Person, person_table)

# added the following method to root controller

@expose() def createsanjay(self):


sanjay = Person(name='sanjay') #sanjay.flush()


return "Can not insert"

return "Inserted!"

The try-except block does not trap the "duplicate name" error. The discussion thread below can be refered for more details.


Change History

comment:1 Changed 13 years ago by alberto

  • Status changed from new to closed
  • Resolution set to fixed

I've committed at [1769] a possible implementation for this using TG's errorhandling. You can now define a function to trap arbitrary exceptions which can occur inside SA's run_with_transaction (or any other controller method as errorhandling has always allowed).

Usage example:

import errorhandling

@errorhandling.dispatch_error.when("isinstance(tg_exceptions, IntegrityError)")
def handle_IE(controller, tg_source, tg_errors, tg_exceptions, *args, **kw):
    log.debug("An IntegrityError (%s) ocurred in method %r of controller %r" %
              tg_exceptions, tg_source, controller)
    turbogears.flash("An integrity error ocurred")
    #XXX: I'm not sure the later will work (tg_source *might* not be decorated by expose)... 
    #     My intention is to re-call the controller method that caused the exception.
    return tg_source(*args, **kw)


comment:2 Changed 13 years ago by alberto

Oops, forget that last tg_source(*args, **kw) line. It'll probably end in an infinite loop as the method keeps causing IntegrityErrors? :). Should probably display a nice error page and/or let the user resubmit the form.

Note: See TracTickets for help on using tickets.