Skip to content

Commit

Permalink
fix: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
keithmanville committed Feb 24, 2025
1 parent 552d14a commit 06ca866
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 19 deletions.
11 changes: 8 additions & 3 deletions src/dioptra/restapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,9 @@ def __init__(self, message: str):
class ImportFailedError(DioptraError):
"""Import failed Error."""

def __init__(self, message: str):
def __init__(self, message: str, reason: str = ""):
super().__init__(message)
self._reason = reason


def error_result(
Expand Down Expand Up @@ -490,10 +491,14 @@ def handle_git_error(error: GitError):
log.debug(error.to_message())
return error_result(error, http.HTTPStatus.INTERNAL_SERVER_ERROR, {})

@api.errorhandler(GitError)
@api.errorhandler(ImportFailedError)
def handle_import_failed_error(error: ImportFailedError):
log.debug(error.to_message())
return error_result(error, http.HTTPStatus.BAD_REQUEST, {})
return error_result(
error,
http.HTTPStatus.BAD_REQUEST,
{"reason": error._reason} if error._reason else {},
)

@api.errorhandler(DioptraError)
def handle_base_error(error: DioptraError):
Expand Down
77 changes: 61 additions & 16 deletions src/dioptra/restapi/v1/workflows/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
Path(__file__).resolve().parent / "dioptra-resources.schema.json"
)

VALID_ENTRYPOINT_PARAM_TYPES: Final[set[str]] = {
"string",
"float",
"integer",
"boolean",
"list",
"mapping",
}


class JobFilesDownloadService(object):
"""The service methods for packaging job files for download."""
Expand Down Expand Up @@ -270,6 +279,16 @@ def import_resources(
try:
with open(working_dir / config_path, "rb") as f:
config = tomllib.load(f)
except tomllib.TOMLDecodeError as e:
raise ImportFailedError(
f"Failed to load resource import config from {config_path}.",
reason=f"Could not decode config: {e}",
) from e
except FileNotFoundError as e:
raise ImportFailedError(
f"Failed to load resource import config from {config_path}.",
reason="File not found.",
) from e
except Exception as e:
raise ImportFailedError(
f"Failed to load resource import config from {config_path}."
Expand Down Expand Up @@ -474,9 +493,17 @@ def _register_entrypoints(
contents = Path(entrypoint["path"]).read_text()
except FileNotFoundError as e:
raise ImportFailedError(
f"Failed to read plugin file from {entrypoint['path']}"
f"Failed to read entrypoint file from {entrypoint['path']}"
) from e

for param in entrypoint.get("params", []):
if param["type"] not in VALID_ENTRYPOINT_PARAM_TYPES:
raise ImportFailedError(
"Invalid entrypoint parameter type provided.",
reason=f"Found '{param['type']}' but must be one of "
f"{VALID_ENTRYPOINT_PARAM_TYPES}.",
)

params = [
{
"name": param["name"],
Expand Down Expand Up @@ -525,25 +552,43 @@ def _build_tasks(

tasks = defaultdict(list)
for task in tasks_config:
try:
input_params = [
{
"name": param["name"],
"parameter_type_id": param_types[param["type"]].resource_id,
"required": param.get("required", False),
}
for param in task["input_params"]
]
except KeyError as e:
raise ImportFailedError(
"Plugin task input parameter type not found.",
reason=f"Parameter type named {e} not does not exist and "
"is not defined in provided toml config.",
) from e

try:
output_params = [
{
"name": param["name"],
"parameter_type_id": param_types[param["type"]].resource_id,
}
for param in task["output_params"]
]
except KeyError as e:
raise ImportFailedError(
"Plugin task output parameter type not found.",
reason=f"Parameter type named {e} not does not exist and "
"is not defined in provided toml config.",
) from e

tasks[task["filename"]].append(
{
"name": task["name"],
"description": task.get("description", None),
"input_params": [
{
"name": param["name"],
"parameter_type_id": param_types[param["type"]].resource_id,
"required": param.get("required", False),
}
for param in task["input_params"]
],
"output_params": [
{
"name": param["name"],
"parameter_type_id": param_types[param["type"]].resource_id,
}
for param in task["output_params"]
],
"input_params": input_params,
"output_params": output_params,
}
)
return tasks

0 comments on commit 06ca866

Please sign in to comment.