From 0ddf2d8db9e040a43522644f159e3d666418aca4 Mon Sep 17 00:00:00 2001 From: Katie Stahl Date: Tue, 24 Sep 2024 14:20:35 -0400 Subject: [PATCH] fix: return correct message when validation error occurs --- src/fusor/fusor.py | 14 +++----------- src/fusor/tools.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/fusor/fusor.py b/src/fusor/fusor.py index c702e92..cbf9319 100644 --- a/src/fusor/fusor.py +++ b/src/fusor/fusor.py @@ -45,7 +45,7 @@ UnknownGeneElement, ) from fusor.nomenclature import generate_nomenclature -from fusor.tools import translate_identifier +from fusor.tools import get_error_message, translate_identifier _logger = logging.getLogger(__name__) @@ -178,11 +178,7 @@ def categorical_fusion( regulatoryElement=regulatory_element, ) except ValidationError as e: - error_message = ( - str(e.errors()[0]["msg"]) - if e.errors() and "msg" in e.errors()[0] - else str(e) - ) + error_message = get_error_message(e) raise FUSORParametersException(error_message) from e return fusion @@ -214,11 +210,7 @@ def assayed_fusion( readingFramePreserved=reading_frame_preserved, ) except ValidationError as e: - error_message = ( - str(e.errors()[0]["msg"]) - if e.errors() and "msg" in e.errors()[0] - else str(e) - ) + error_message = get_error_message(e) raise FUSORParametersException(error_message) from e return fusion diff --git a/src/fusor/tools.py b/src/fusor/tools.py index bb07695..9fb2334 100644 --- a/src/fusor/tools.py +++ b/src/fusor/tools.py @@ -9,6 +9,7 @@ from gene.database import AbstractDatabase as GeneDatabase from gene.database import create_db from gene.schemas import CURIE +from pydantic import ValidationError from fusor.exceptions import IDTranslationException @@ -89,3 +90,14 @@ async def check_data_resources( return FusorDataResourceStatus( cool_seq_tool=all(cst_status), gene_normalizer=gene_status ) + + +def get_error_message(e: ValidationError) -> str: + """Get the specific error message from a pydantic ValidationError + + :param e: the ValidationError to get the message from + :return: extracted error message or the string representation of the exception if 'msg' field is not present + """ + return ( + str(e.errors()[0]["msg"]) if e.errors() and "msg" in e.errors()[0] else str(e) + )