Skip to content

Commit 6b86fa0

Browse files
committed
Fix:Error message for invalid task ID
1 parent 954a43a commit 6b86fa0

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- `fill` and `fill_structures` argument in `td.Simulation.plot_structures()` and `td.Simulation.plot()` respectively to disable fill and plot outlines of structures only.
1212
- New subpixel averaging option `ContourPathAveraging` applied to dielectric material boundaries.
1313

14+
### Changed
15+
- Error message for invalid task ID.
16+
1417
### Fixed
1518
- Compatibility with `xarray>=2025.03`.
1619

Diff for: tests/test_web/test_webapi.py

+16
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
upload,
4040
)
4141
from tidy3d.web.core.environment import Env
42+
from tidy3d.web.core.exceptions import WebNotFoundError
4243
from tidy3d.web.core.types import TaskType
4344

4445
TASK_NAME = "task_name_test"
@@ -722,3 +723,18 @@ def save_sim_to_path(path: str) -> None:
722723
"--inspect_sim",
723724
]
724725
)
726+
727+
728+
@responses.activate
729+
def test_load_invalid_task_raises(mock_webapi):
730+
"""Ensure that load() raises TaskNotFoundError for a non-existent task ID."""
731+
fake_id = "INVALID_TASK_ID"
732+
733+
responses.add(
734+
responses.GET,
735+
f"{Env.current.web_api_endpoint}/tidy3d/tasks/{fake_id}/detail",
736+
json={"error": "Task not found"},
737+
status=404,
738+
)
739+
with pytest.raises(WebNotFoundError, match="Resource not found"):
740+
load(fake_id)

Diff for: tidy3d/web/core/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ def __init__(self, message: str = None):
1111
log = get_logger()
1212
super().__init__(message)
1313
log.error(message)
14+
15+
16+
class WebNotFoundError(WebError):
17+
"""A generic error indicating an HTTP 404 (resource not found)."""
18+
19+
pass

Diff for: tidy3d/web/core/http_util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
from .core_config import get_logger
2727
from .environment import Env
28-
from .exceptions import WebError
28+
from .exceptions import WebError, WebNotFoundError
2929

3030
REINITIALIZED = False
3131

@@ -131,7 +131,7 @@ def wrapper(*args, **kwargs):
131131

132132
if resp.status_code != ResponseCodes.OK.value:
133133
if resp.status_code == ResponseCodes.NOT_FOUND.value:
134-
return None
134+
raise WebNotFoundError("Resource not found (HTTP 404).")
135135
json_resp = resp.json()
136136
if "error" in json_resp.keys():
137137
raise WebError(json_resp["error"])

Diff for: tidy3d/web/core/task_core.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
from botocore.exceptions import ClientError
1313
from pydantic.v1 import Extra, Field, parse_obj_as
1414

15+
import tidy3d as td
16+
1517
from . import http_util
1618
from .cache import FOLDER_CACHE
1719
from .constants import SIM_ERROR_FILE, SIM_FILE_HDF5_GZ, SIM_LOG_FILE, SIMULATION_DATA_HDF5_GZ
1820
from .core_config import get_logger_console
1921
from .environment import Env
20-
from .exceptions import WebError
22+
from .exceptions import WebError, WebNotFoundError
2123
from .file_util import read_simulation_from_hdf5
2224
from .http_util import http
2325
from .s3utils import download_file, download_gz_file, upload_file
@@ -261,7 +263,12 @@ def get(cls, task_id: str, verbose: bool = True) -> SimulationTask:
261263
:class:`.SimulationTask` object containing info about status,
262264
size, credits of task and others.
263265
"""
264-
resp = http.get(f"tidy3d/tasks/{task_id}/detail")
266+
try:
267+
resp = http.get(f"tidy3d/tasks/{task_id}/detail")
268+
except WebNotFoundError as e:
269+
td.log.error(f"The requested task ID '{task_id}' does not exist.")
270+
raise e
271+
265272
task = SimulationTask(**resp) if resp else None
266273
return task
267274

0 commit comments

Comments
 (0)