Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(variant): add reason when variant generation fails #2290

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -42,7 +42,7 @@
from antarest.core.filetransfer.model import FileDownloadTaskDTO
from antarest.core.interfaces.cache import ICache
from antarest.core.interfaces.eventbus import Event, EventChannelDirectory, EventType, IEventBus
from antarest.core.jwt import DEFAULT_ADMIN_USER, JWTUser
from antarest.core.jwt import DEFAULT_ADMIN_USER
from antarest.core.model import JSON, PermissionInfo, PublicMode, StudyPermissionType
from antarest.core.requests import RequestParameters, UserHasNotPermissionError
from antarest.core.serialization import to_json_string
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
Loading