Commit a08db79dfde4b1af92586d2e2d855ef4cfcd6f61
1 parent
66a8c53f
Exists in
master
and in
1 other branch
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 | 10 | import logging |
| 11 | 11 | from os import path |
| 12 | 12 | |
| 13 | -# user installed packages | |
| 13 | +# installed packages | |
| 14 | 14 | import bcrypt |
| 15 | -from sqlalchemy import create_engine | |
| 15 | +from sqlalchemy import create_engine, exc | |
| 16 | 16 | from sqlalchemy.orm import sessionmaker |
| 17 | 17 | |
| 18 | 18 | # this project |
| ... | ... | @@ -73,9 +73,10 @@ class App(): |
| 73 | 73 | try: |
| 74 | 74 | yield session |
| 75 | 75 | session.commit() |
| 76 | - except Exception: | |
| 76 | + except exc.SQLAlchemyError: | |
| 77 | 77 | logger.error('DB rollback!!!') |
| 78 | 78 | session.rollback() |
| 79 | + raise | |
| 79 | 80 | finally: |
| 80 | 81 | session.close() |
| 81 | 82 | |
| ... | ... | @@ -359,7 +360,7 @@ class App(): |
| 359 | 360 | try: |
| 360 | 361 | with self.db_session() as sess: |
| 361 | 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 | 365 | else: |
| 365 | 366 | logger.info('New student inserted: %s, %s', uid, name) | ... | ... |