Skip to content

More readable error message on missing files for capture ops #75

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 9 additions & 6 deletions gateway/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ build:
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) pull --ignore-buildable
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) build $(ARGS)

up:
@echo "Starting sds-gateway"
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) up -d $(ARGS)
down:
@echo "Stopping sds-gateway"
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) down $(ARGS)

logs:
@echo "Showing sds-gateway logs…"
Expand All @@ -32,9 +32,8 @@ logs-once:
@echo "Showing gateway logs once…"
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) logs $(ARGS)

down:
@echo "Stopping sds-gateway"
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) down $(ARGS)
pre-commit:
uv run --dev pre-commit run --all-files

restart:
@echo "Restarting sds-gateway"
Expand All @@ -44,3 +43,7 @@ test:
@echo "Running tests"
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) run $(APP_CONTAINER) pytest
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) run $(APP_CONTAINER) python manage.py test --force-color

up:
@echo "Starting sds-gateway"
@COMPOSE_FILE=$(COMPOSE_FILE) docker compose --env-file $(ENV_FILE) up -d $(ARGS)
1 change: 1 addition & 0 deletions gateway/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@

[tool.ruff.lint]
ignore = [
# https://docs.astral.sh/ruff/settings/#lint_ignore
"N811", # Constant imports aliased to non-constant-style names (false positives with django)
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
"S101", # Use of assert detected https://docs.astral.sh/ruff/rules/assert/
Expand Down
61 changes: 44 additions & 17 deletions gateway/sds_gateway/api_methods/views/capture_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ def ingest_capture(
requester: The user making the request
rh_scan_group: Optional scan group UUID for RH captures
top_level_dir: Path to directory containing files to connect to capture
Raise:
FileNotFoundError: If there are no files to connect to this capture
"""

# Handle file connections if top_level_dir provided
if top_level_dir:
with tempfile.TemporaryDirectory() as temp_dir:
# Reconstruct the file tree in a temporary directory
# reconstruct the file tree in a temporary directory
tmp_dir_path, files_to_connect = reconstruct_tree(
target_dir=Path(temp_dir),
virtual_top_dir=top_level_dir,
Expand All @@ -132,7 +134,12 @@ def ingest_capture(
rh_scan_group=rh_scan_group,
)

# Connect the files to the capture
if not files_to_connect:
msg = f"No files found for capture '{capture.uuid}'"
log.warning(msg)
raise FileNotFoundError(msg)

# connect the files to the capture
for cur_file in files_to_connect:
cur_file.capture = capture
cur_file.save()
Expand Down Expand Up @@ -234,21 +241,31 @@ def create(self, request: Request) -> Response: # noqa: PLR0911
requester=requester,
top_level_dir=requested_top_level_dir,
)
except UnknownIndexError as e:
user_msg = f"Unknown index: '{e}'. Try recreating this capture."
except UnknownIndexError as err:
user_msg = f"Unknown index: '{err}'. Try recreating this capture."
server_msg = (
f"Unknown index: '{e}'. Try running the init_indices "
f"Unknown index: '{err}'. Try running the init_indices "
"subcommand if this is index should exist."
)
log.error(server_msg)
capture.soft_delete()
return Response({"detail": user_msg}, status=status.HTTP_400_BAD_REQUEST)
except ValueError as e:
user_msg = f"Error handling metadata for capture '{capture.uuid}': {e}"
except FileNotFoundError as err:
user_msg = (
"Could not find relevant files to create capture. "
f"Please, check if your files are in '{unsafe_top_level_dir}' "
"under SDS. You can list the files in this directory to verify."
f" {err}"
)
log.warning(user_msg)
capture.soft_delete()
return Response({"detail": user_msg}, status=status.HTTP_400_BAD_REQUEST)
except ValueError as err:
user_msg = f"Error handling metadata for capture '{capture.uuid}': {err}"
capture.soft_delete()
return Response({"detail": user_msg}, status=status.HTTP_400_BAD_REQUEST)
except os_exceptions.ConnectionError as e:
user_msg = f"Error connecting to OpenSearch: {e}"
except os_exceptions.ConnectionError as err:
user_msg = f"Error connecting to OpenSearch: {err}"
log.error(user_msg)
capture.soft_delete()
return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE)
Expand Down Expand Up @@ -451,7 +468,7 @@ def list(self, request: Request) -> Response:
),
summary="Update Capture",
)
def update(self, request: Request, pk: str | None = None) -> Response:
def update(self, request: Request, pk: str | None = None) -> Response: # noqa: PLR0911
"""Update a capture by adding files or re-indexing metadata."""
if pk is None:
return Response(
Expand Down Expand Up @@ -485,19 +502,29 @@ def update(self, request: Request, pk: str | None = None) -> Response:
requester=owner,
top_level_dir=requested_top_level_dir,
)
except UnknownIndexError as e:
user_msg = f"Unknown index: '{e}'. Try recreating this capture."
except UnknownIndexError as err:
user_msg = f"Unknown index: '{err}'. Try recreating this capture."
server_msg = (
f"Unknown index: '{e}'. Try running the init_indices "
f"Unknown index: '{err}'. Try running the init_indices "
"subcommand if this is index should exist."
)
log.error(server_msg)
return Response({"detail": user_msg}, status=status.HTTP_400_BAD_REQUEST)
except ValueError as e:
msg = f"Error handling metadata for capture '{target_capture.uuid}': {e}"
except FileNotFoundError as err:
user_msg = (
"Could not find relevant files to update capture. "
"Please, check if your files are still in "
f"'{target_capture.top_level_dir}' "
"under SDS. You can list the files in this directory to verify."
f" {err}"
)
log.warning(user_msg)
return Response({"detail": user_msg}, status=status.HTTP_400_BAD_REQUEST)
except ValueError as err:
msg = f"Error handling metadata for capture '{target_capture.uuid}': {err}"
return Response({"detail": msg}, status=status.HTTP_400_BAD_REQUEST)
except os_exceptions.ConnectionError as e:
msg = f"Error connecting to OpenSearch: {e}"
except os_exceptions.ConnectionError as err:
msg = f"Error connecting to OpenSearch: {err}"
log.error(msg)
return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE)

Expand Down
2 changes: 1 addition & 1 deletion sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@
# https://docs.astral.sh/ruff/settings/#lint_ignore
"COM812", # disabled following ruff's recommendation
"ISC001", # disabled following ruff's recommendation
# "N811", # Constant imports aliased to non-constant-style names (false positives with django)
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
"S101", # Use of assert detected https://docs.astral.sh/ruff/rules/assert/
"S104", # Possible binding to all interfaces
"SIM102", # sometimes it's better to nest
"UP038", # Checks for uses of isinstance/issubclass that take a tuple
# of types for comparison.
# "N811", # Constant imports aliased to non-constant-style names (false positives with django)
# UP038 deactivated because it can make the code slow:
# https://github.com/astral-sh/ruff/issues/7871
]
Expand Down