clean up sqlalchemy exception code

just a little more cleanup. The same as #5299