Skip to content

Fix:Error message for invalid task ID #2345

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

Open
wants to merge 1 commit into
base: develop
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `fill` and `fill_structures` argument in `td.Simulation.plot_structures()` and `td.Simulation.plot()` respectively to disable fill and plot outlines of structures only.
- New subpixel averaging option `ContourPathAveraging` applied to dielectric material boundaries.

### Changed
- Error message for invalid task ID.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved error message and handling when attempting to load a non-existent task ID.


### Fixed
- Compatibility with `xarray>=2025.03`.

Expand Down
16 changes: 16 additions & 0 deletions tests/test_web/test_webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
upload,
)
from tidy3d.web.core.environment import Env
from tidy3d.web.core.exceptions import WebNotFoundError
from tidy3d.web.core.types import TaskType

TASK_NAME = "task_name_test"
Expand Down Expand Up @@ -722,3 +723,18 @@ def save_sim_to_path(path: str) -> None:
"--inspect_sim",
]
)


@responses.activate
def test_load_invalid_task_raises(mock_webapi):
"""Ensure that load() raises TaskNotFoundError for a non-existent task ID."""
fake_id = "INVALID_TASK_ID"

responses.add(
responses.GET,
f"{Env.current.web_api_endpoint}/tidy3d/tasks/{fake_id}/detail",
json={"error": "Task not found"},
status=404,
)
with pytest.raises(WebNotFoundError, match="Resource not found"):
load(fake_id)
6 changes: 6 additions & 0 deletions tidy3d/web/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ def __init__(self, message: str = None):
log = get_logger()
super().__init__(message)
log.error(message)


class WebNotFoundError(WebError):
"""A generic error indicating an HTTP 404 (resource not found)."""

pass
4 changes: 2 additions & 2 deletions tidy3d/web/core/http_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from .core_config import get_logger
from .environment import Env
from .exceptions import WebError
from .exceptions import WebError, WebNotFoundError

REINITIALIZED = False

Expand Down Expand Up @@ -131,7 +131,7 @@ def wrapper(*args, **kwargs):

if resp.status_code != ResponseCodes.OK.value:
if resp.status_code == ResponseCodes.NOT_FOUND.value:
return None
raise WebNotFoundError("Resource not found (HTTP 404).")
json_resp = resp.json()
if "error" in json_resp.keys():
raise WebError(json_resp["error"])
Expand Down
11 changes: 9 additions & 2 deletions tidy3d/web/core/task_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from botocore.exceptions import ClientError
from pydantic.v1 import Extra, Field, parse_obj_as

import tidy3d as td

from . import http_util
from .cache import FOLDER_CACHE
from .constants import SIM_ERROR_FILE, SIM_FILE_HDF5_GZ, SIM_LOG_FILE, SIMULATION_DATA_HDF5_GZ
from .core_config import get_logger_console
from .environment import Env
from .exceptions import WebError
from .exceptions import WebError, WebNotFoundError
from .file_util import read_simulation_from_hdf5
from .http_util import http
from .s3utils import download_file, download_gz_file, upload_file
Expand Down Expand Up @@ -261,7 +263,12 @@ def get(cls, task_id: str, verbose: bool = True) -> SimulationTask:
:class:`.SimulationTask` object containing info about status,
size, credits of task and others.
"""
resp = http.get(f"tidy3d/tasks/{task_id}/detail")
try:
resp = http.get(f"tidy3d/tasks/{task_id}/detail")
except WebNotFoundError as e:
td.log.error(f"The requested task ID '{task_id}' does not exist.")
raise e

task = SimulationTask(**resp) if resp else None
return task

Expand Down