Skip to content

Commit d46649b

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

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixed
1515
- Compatibility with `xarray>=2025.03`.
1616

17+
### Changed
18+
- Error message for invalid task ID.
19+
1720
## [2.8.1] - 2025-03-20
1821

1922
### Added

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 TaskNotFoundError
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(TaskNotFoundError, match="does not exist"):
740+
load(fake_id)

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

+12
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ def __init__(self, message: str = None):
1111
log = get_logger()
1212
super().__init__(message)
1313
log.error(message)
14+
15+
16+
class TaskNotFoundError(WebError):
17+
"""Raised when a requested task ID does not exist on the server."""
18+
19+
pass
20+
21+
22+
class WebNotFoundError(WebError):
23+
"""A generic error indicating an HTTP 404 (resource not found)."""
24+
25+
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .constants import SIM_ERROR_FILE, SIM_FILE_HDF5_GZ, SIM_LOG_FILE, SIMULATION_DATA_HDF5_GZ
1818
from .core_config import get_logger_console
1919
from .environment import Env
20-
from .exceptions import WebError
20+
from .exceptions import TaskNotFoundError, WebError, WebNotFoundError
2121
from .file_util import read_simulation_from_hdf5
2222
from .http_util import http
2323
from .s3utils import download_file, download_gz_file, upload_file
@@ -261,7 +261,10 @@ def get(cls, task_id: str, verbose: bool = True) -> SimulationTask:
261261
:class:`.SimulationTask` object containing info about status,
262262
size, credits of task and others.
263263
"""
264-
resp = http.get(f"tidy3d/tasks/{task_id}/detail")
264+
try:
265+
resp = http.get(f"tidy3d/tasks/{task_id}/detail")
266+
except WebNotFoundError:
267+
raise TaskNotFoundError(f"The requested task ID '{task_id}' does not exist.")
265268
task = SimulationTask(**resp) if resp else None
266269
return task
267270

0 commit comments

Comments
 (0)