diff --git a/editor-api/archive_editor/data_model.py b/editor-api/archive_editor/data_model.py index 4c91849..d669204 100644 --- a/editor-api/archive_editor/data_model.py +++ b/editor-api/archive_editor/data_model.py @@ -1,6 +1,7 @@ from dataclasses import dataclass, asdict, field -from pydantic import BaseModel as Base, create_model, ConfigDict +from pydantic import BaseModel as Base, create_model, ConfigDict, field_validator from typing import Union, Dict, List, Tuple +from fastapi import HTTPException from biosimulators_utils.combine.data_model import CombineArchive from biosimulators_utils.sedml.data_model import SedDocument, Model, Simulation @@ -31,7 +32,22 @@ class SerializedParametersBase(BaseModel): pass -class SimulationEditConfirmation(BaseModel): +class ResultConfirmation(BaseModel): + message: str + + +class UnsuccessfulSimulationEditConfirmation(ResultConfirmation): + message: str = "Could not successfully edit the simulation" + exception: Exception + error: HTTPException + + @field_validator('error') + @classmethod + def raise_error(cls, error: HTTPException): + return error(status_code=500, detail=str(cls.exception)) + + +class SuccessfulSimulationEditConfirmation(BaseModel): message: str = "Simulation edited successfully." download_link: str diff --git a/editor-api/archive_editor/main.py b/editor-api/archive_editor/main.py index f94cc8d..9e07bd6 100644 --- a/editor-api/archive_editor/main.py +++ b/editor-api/archive_editor/main.py @@ -9,7 +9,11 @@ from tempfile import TemporaryDirectory, mkdtemp import shutil # For copying files from archive_editor.api import ArchiveEditorApi -from archive_editor.data_model import SimulationEditConfirmation, SimulationEditResult +from archive_editor.data_model import ( + SuccessfulSimulationEditConfirmation, + UnsuccessfulSimulationEditConfirmation, + SimulationEditResult +) app = FastAPI(title="editor-api", version="1.0.0") @@ -51,9 +55,13 @@ async def edit_simulation( edited_file_path = os.path.join(edited_files_storage, f"{new_omex_filename}.omex") shutil.move(file_path, edited_file_path) - return SimulationEditConfirmation(download_link=f"/download/{edited_file_path}") + return SuccessfulSimulationEditConfirmation(download_link=f"/download/{edited_file_path}") except Exception as e: - raise HTTPException(status_code=500, detail=str(e)) + result = UnsuccessfulSimulationEditConfirmation( + error=HTTPException(status_code=500, detail=str(e)), + exception=e) + # raise HTTPException(status_code=500, detail=str(e)) + raise result.error @app.get("/download/{file_identifier}")