<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">In my project I need to catch and process an integrity exceptions, because user want to see a understandable error message instead of framework's exception (or just http 500 error). So the easiest way I found was something like this (let's say i want to process only duplicate errors. <code class="notranslate">obj</code> is object to save and <code class="notranslate">table</code> is table, mapped to obj's class):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="errors = {}
try:
    Session.flush([obj](obj))
except sqlalchemy.exceptions.IntegrityError, e:
    err = str(e.orig)
    m = re.match(r'^column (\w+) is not unique$', err, re.I)
    if not m: raise e
    dupcol = m.groups(0)[0](0)
    del m
    if not dupcol in table.c: raise e
    dupcol = table.c[dupcol](dupcol)
    errors[dupcol](dupcol) = IntegrityError(dupcol.unique)"><pre class="notranslate"><code class="notranslate">errors = {}
try:
    Session.flush([obj](obj))
except sqlalchemy.exceptions.IntegrityError, e:
    err = str(e.orig)
    m = re.match(r'^column (\w+) is not unique$', err, re.I)
    if not m: raise e
    dupcol = m.groups(0)[0](0)
    del m
    if not dupcol in table.c: raise e
    dupcol = table.c[dupcol](dupcol)
    errors[dupcol](dupcol) = IntegrityError(dupcol.unique)
</code></pre></div>
<p dir="auto">It is not clear and simple way, is it?</p>
<p dir="auto">I am sorry for my English.</p>
<p dir="auto"><strong>Migrated issue, originally created by Michael Bayer (<a href="https://github.com/zzzeek">@zzzeek</a>)</strong></p>
<p dir="auto">we had someone wanting postgres SQLSTATE values to be propagated.  i could not find any psycopg documentation at all about this being available, but then someone just posted an example that illustrated the undocumented psycopg attribute that makes this available.</p>
<p dir="auto">the <code class="notranslate">SQLError</code> exception should have a "code" attribute that tries to get at the SQLSTATE/error number reported by the database, if possible.  and maybe also "message".  this way people wont need to drill into the db-specific exception itself.  will need to build hooks into the dialect to populate this value when sql exceptions are raised.</p>
<p dir="auto">the hard part here is identifying how all the dbapis report the code/message...im a little skeptical if they even all have codes.</p>