Improved error messaging when saving submissions.
This commit is contained in:
@@ -6,10 +6,11 @@ import sys, logging
|
||||
from sqlalchemy import Column, INTEGER, String, JSON
|
||||
from sqlalchemy.orm import DeclarativeMeta, declarative_base, Query, Session
|
||||
from sqlalchemy.ext.declarative import declared_attr
|
||||
from sqlalchemy.exc import ArgumentError
|
||||
from sqlalchemy.exc import ArgumentError, IntegrityError as sqlalcIntegrityError
|
||||
from typing import Any, List
|
||||
from pathlib import Path
|
||||
from tools import report_result
|
||||
# from sqlite3 import IntegrityError as sqliteIntegrityError
|
||||
|
||||
# Load testing environment
|
||||
if 'pytest' in sys.modules:
|
||||
@@ -165,8 +166,21 @@ class BaseClass(Base):
|
||||
self.__database_session__.commit()
|
||||
except Exception as e:
|
||||
logger.critical(f"Problem saving object: {e}")
|
||||
logger.error(f"Error message: {type(e)}")
|
||||
match e:
|
||||
case sqlalcIntegrityError():
|
||||
origin = e.orig.__str__().lower()
|
||||
logger.debug(f"Exception origin: {origin}")
|
||||
if "unique constraint failed:" in origin:
|
||||
field = origin.split(".")[1].replace("_", " ").upper()
|
||||
logger.debug(field)
|
||||
msg = f"{field} doesn't have a unique value.\nIt must be changed."
|
||||
else:
|
||||
msg = f"Got unknown integrity error: {e}"
|
||||
case _:
|
||||
msg = f"Got generic error: {e}"
|
||||
self.__database_session__.rollback()
|
||||
report.add_result(Result(msg=f"Problem saving object {e}", status="Critical"))
|
||||
report.add_result(Result(msg=msg, status="Critical"))
|
||||
return report
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user