Skip to content

Commit

Permalink
Update error handling in webapi, and Heat->HeatCharge in message
Browse files Browse the repository at this point in the history
  • Loading branch information
momchil-flex committed Jan 27, 2025
1 parent 948098a commit f50d1e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/notebooks
25 changes: 20 additions & 5 deletions tidy3d/web/api/webapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Provides lowest level, user-facing interface to server."""

import json
import os
import tempfile
import time
from datetime import datetime, timedelta
from typing import Callable, Dict, List
Expand Down Expand Up @@ -46,7 +48,7 @@
BETA_TASK_TYPES = ["HEAT", "EME"]

# map task_type to solver name for display
SOLVER_NAME = {"FDTD": "FDTD", "HEAT": "Heat", "MODE_SOLVER": "Mode", "EME": "EME"}
SOLVER_NAME = {"FDTD": "FDTD", "HEAT": "HeatCharge", "MODE_SOLVER": "Mode", "EME": "EME"}


def _get_url(task_id: str) -> str:
Expand Down Expand Up @@ -405,10 +407,19 @@ def get_status(task_id) -> str:
if status == "visualize":
return "success"
if status == "error":
raise WebError(
f"Error running task {task_id}! Use 'web.download_log(task_id)' to "
"download and examine the solver log, and/or contact customer support for help."
)
try:
# Try to obtain the error message
task = SimulationTask(taskId=task_id)
with tempfile.NamedTemporaryFile(suffix=".json") as tmp_file:
task.get_error_json(to_file=tmp_file.name)
with open(tmp_file.name) as f:
error_content = json.load(f)
error_msg = error_content["msg"]
except Exception:
# If the error message could not be obtained, raise a generic error message
error_msg = "Error message could not be obtained, please contact customer support."

raise WebError(f"Error running task {task_id}! {error_msg}")
return status


Expand Down Expand Up @@ -547,6 +558,10 @@ def monitor_preprocess() -> None:
perc_done, _ = get_run_info(task_id)
new_description = "solver progress"
progress.update(pbar_pd, completed=100, refresh=True, description=new_description)
else:
while get_status(task_id) == "running":
perc_done, _ = get_run_info(task_id)
time.sleep(1.0)

else:
# non-verbose case, just keep checking until status is not running or perc_done >= 100
Expand Down
1 change: 1 addition & 0 deletions tidy3d/web/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
SIM_FILE_HDF5_GZ = "simulation.hdf5.gz"
MODE_FILE_HDF5_GZ = "mode_solver.hdf5.gz"
MODE_DATA_HDF5_GZ = "output/mode_solver_data.hdf5.gz"
SIM_ERROR_FILE = "output/tidy3d_error.json"
35 changes: 30 additions & 5 deletions tidy3d/web/core/task_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import http_util
from .cache import FOLDER_CACHE
from .constants import SIM_FILE_HDF5_GZ, SIM_LOG_FILE, SIMULATION_DATA_HDF5_GZ
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
Expand Down Expand Up @@ -292,7 +292,7 @@ def get_simulation_json(self, to_file: str, verbose: bool = True) -> pathlib.Pat
Parameters
----------
to_file: str
save file to path.
Save file to path.
verbose: bool = True
Whether to display progress bars.
Expand Down Expand Up @@ -469,7 +469,7 @@ def get_sim_data_hdf5(
Parameters
----------
to_file: str
save file to path.
Save file to path.
verbose: bool = True
Whether to display progress bars.
progress_callback : Callable[[float], None] = None
Expand Down Expand Up @@ -526,7 +526,7 @@ def get_simulation_hdf5(
Parameters
----------
to_file: str
save file to path.
Save file to path.
verbose: bool = True
Whether to display progress bars.
progress_callback : Callable[[float], None] = None
Expand Down Expand Up @@ -576,7 +576,7 @@ def get_log(
Parameters
----------
to_file: str
save file to path.
Save file to path.
verbose: bool = True
Whether to display progress bars.
progress_callback : Callable[[float], None] = None
Expand All @@ -599,6 +599,31 @@ def get_log(
progress_callback=progress_callback,
)

def get_error_json(self, to_file: str, verbose: bool = True) -> pathlib.Path:
"""Get error json file for a :class:`.Simulation` from server.
Parameters
----------
to_file: str
Save file to path.
verbose: bool = True
Whether to display progress bars.
Returns
-------
path: pathlib.Path
Path to saved file.
"""
if not self.task_id:
raise WebError("Expected field 'task_id' is unset.")

return download_file(
self.task_id,
SIM_ERROR_FILE,
to_file=to_file,
verbose=verbose,
)

def abort(self):
"""Abort current task from server."""
if not self.task_id:
Expand Down

0 comments on commit f50d1e6

Please sign in to comment.