Paul Howell wrote:
>
>     The same problem exists for other databases (SQLite, etc).  When I 
> ran into this problem earlier, I grepped code until I found that 
> SQLObject (not SQLAlchemy, but probably a similar situation) ONLY 
> returns an SQLObjectNotFound error... that's just about the only DB 
> error it traps, if no object is found by the query.  Otherwise it just 
> passes the native RDBMS error message thru.
>    Each RDBMS behind these ORMs returns different errors, therefore it 
> is difficult to trap them all in a try:except pattern... [...]

With >=SQLObject-0.10.x (or even with 0.9, but I am not quite sure about this)
this is not longer true. You can write something like this utilizing the new
exceptions (here DuplicateEntryError):

    @tg.expose(...)
    @tg.validate(...)
    def add(self, contact_id, language, tg_errors=None):
        if tg_errors:
            ...
        try:
            c = Contact.get(contact_id)
            l = Language(contact=c, language=language)
            return dict(success=True,
                        lang_id=l.id,
                        ...)
        except SQLObjectNotFound:
            return dict(success=False,
                        error=_("The given contact does not exist anymore."))
        except DuplicateEntryError:                          # here!
            return dict(success=False,
                        error=_("That language has already been added."))
        except ...

-- Mark

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to