Skip to content

Commit

Permalink
fix: return correct message when validation error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
katiestahl committed Sep 24, 2024
1 parent 951c7d3 commit 0ddf2d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/fusor/fusor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/fusor/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
)

0 comments on commit 0ddf2d8

Please sign in to comment.