Skip to content

Commit

Permalink
feat(variant): add reason when variant generation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Jan 10, 2025
1 parent 5f4adff commit 26de9d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 13 additions & 5 deletions antarest/study/storage/variantstudy/variant_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from antarest.core.tasks.model import CustomTaskEventMessages, TaskDTO, TaskResult, TaskType
from antarest.core.tasks.service import DEFAULT_AWAIT_MAX_TIMEOUT, ITaskNotifier, ITaskService, NoopNotifier
from antarest.core.utils.fastapi_sqlalchemy import db
from antarest.core.utils.utils import assert_this, suppress_exception
from antarest.core.utils.utils import UUID_PATTERN, assert_this, suppress_exception
from antarest.login.model import Identity
from antarest.matrixstore.service import MatrixService
from antarest.study.model import RawStudy, Study, StudyAdditionalData, StudyMetadataDTO, StudySimResultDTO
Expand Down Expand Up @@ -971,20 +971,28 @@ def _safe_generation(self, metadata: VariantStudy, timeout: int = DEFAULT_AWAIT_
task_id = self.generate_task(metadata)
self.task_service.await_task(task_id, timeout)
result = self.task_service.status_task(task_id, RequestParameters(DEFAULT_ADMIN_USER))
if result.result and result.result.success:
if not result.result:
raise ValueError("No task result")
if result.result.success:
# OK, the study has been generated
return
raise ValueError("No task result or result failed")
# The variant generation failed, we have to raise a clear exception.
error_msg = result.result.message
stripped_msg = error_msg.removeprefix(
f"Task {task_id} failed: Unhandled exception 417: Failed to generate variant study {metadata.id}"
)
final_msg = stripped_msg.splitlines()[0]
raise ValueError(final_msg)

except concurrent.futures.TimeoutError as e:
# Raise a REQUEST_TIMEOUT error (408)
logger.error(f"⚡ Timeout while generating variant study {metadata.id}", exc_info=e)
raise VariantGenerationTimeoutError(f"Timeout while generating {metadata.id}") from None
raise VariantGenerationTimeoutError(f"Timeout while generating variant {metadata.id}") from None

except Exception as e:
# raise a EXPECTATION_FAILED error (417)
logger.error(f"⚡ Fail to generate variant study {metadata.id}", exc_info=e)
raise VariantGenerationError(f"Error while generating {metadata.id}") from None
raise VariantGenerationError(f"Error while generating variant {metadata.id} {e.args[0]}") from None

@staticmethod
def _get_snapshot_last_executed_command_index(
Expand Down
5 changes: 1 addition & 4 deletions tests/storage/business/test_variant_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ def test_get(tmp_path: str, project_path) -> None:
data = {"titi": 43}
sub_route = "settings"

path = path_study / "settings"
key = "titi"

study = Mock()
study.get.return_value = data
study_factory = Mock()
Expand Down Expand Up @@ -104,7 +101,7 @@ def task_status(*args):
yield t

study_service.task_service.status_task.side_effect = task_status()
with pytest.raises(VariantGenerationError, match="Error while generating study2.py"):
with pytest.raises(VariantGenerationError, match="Error while generating variant study2.py"):
study_service.get(metadata=metadata, url=sub_route, depth=2)
study_service.task_service.await_task.assert_called()

Expand Down

0 comments on commit 26de9d2

Please sign in to comment.