Commit a08db79dfde4b1af92586d2e2d855ef4cfcd6f61

Authored by Miguel Barão
1 parent 66a8c53f
Exists in master and in 1 other branch dev

fix exception handling of sqlalchemy

Showing 1 changed file with 6 additions and 5 deletions   Show diff stats
perguntations/app.py
@@ -10,9 +10,9 @@ import json @@ -10,9 +10,9 @@ import json
10 import logging 10 import logging
11 from os import path 11 from os import path
12 12
13 -# user installed packages 13 +# installed packages
14 import bcrypt 14 import bcrypt
15 -from sqlalchemy import create_engine 15 +from sqlalchemy import create_engine, exc
16 from sqlalchemy.orm import sessionmaker 16 from sqlalchemy.orm import sessionmaker
17 17
18 # this project 18 # this project
@@ -73,9 +73,10 @@ class App(): @@ -73,9 +73,10 @@ class App():
73 try: 73 try:
74 yield session 74 yield session
75 session.commit() 75 session.commit()
76 - except Exception: 76 + except exc.SQLAlchemyError:
77 logger.error('DB rollback!!!') 77 logger.error('DB rollback!!!')
78 session.rollback() 78 session.rollback()
  79 + raise
79 finally: 80 finally:
80 session.close() 81 session.close()
81 82
@@ -359,7 +360,7 @@ class App(): @@ -359,7 +360,7 @@ class App():
359 try: 360 try:
360 with self.db_session() as sess: 361 with self.db_session() as sess:
361 sess.add(Student(id=uid, name=name, password='')) 362 sess.add(Student(id=uid, name=name, password=''))
362 - except Exception:  
363 - logger.error('Insert failed: student %s already exists.', uid) 363 + except exc.SQLAlchemyError:
  364 + logger.error('Insert failed: student %s already exists?', uid)
364 else: 365 else:
365 logger.info('New student inserted: %s, %s', uid, name) 366 logger.info('New student inserted: %s, %s', uid, name)