Skip to content
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

Handle get_db_object() failures as DownloadException #2276

Merged
merged 1 commit into from
Nov 1, 2024
Merged
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
21 changes: 16 additions & 5 deletions client/securedrop_client/api_jobs/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tempfile import NamedTemporaryFile
from typing import Any

from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.session import Session

from securedrop_client.api_jobs.base import SingleObjectApiJob
Expand Down Expand Up @@ -108,9 +109,10 @@ def call_decrypt(self, filepath: str, session: Session | None = None) -> str:
"""
raise NotImplementedError

def get_db_object(self, session: Session) -> File | Message:
def get_db_object(self, session: Session) -> File | Message | Reply:
"""
Get the database object associated with this job.
Get the database object associated with this job; may raise
DownloadException if not found
"""
raise NotImplementedError

Expand Down Expand Up @@ -242,7 +244,10 @@ def get_db_object(self, session: Session) -> Reply:
"""
Override DownloadJob.
"""
return session.query(Reply).filter_by(uuid=self.uuid).one()
try:
return session.query(Reply).filter_by(uuid=self.uuid).one()
except NoResultFound:
raise DownloadException("Reply not found in database", Reply, self.uuid)

def call_download_api(self, api: API, db_object: Reply) -> tuple[str, str]:
"""
Expand Down Expand Up @@ -298,7 +303,10 @@ def get_db_object(self, session: Session) -> Message:
"""
Override DownloadJob.
"""
return session.query(Message).filter_by(uuid=self.uuid).one()
try:
return session.query(Message).filter_by(uuid=self.uuid).one()
except NoResultFound:
raise DownloadException("Message not found in database", Message, self.uuid)

def call_download_api(self, api: API, db_object: Message) -> tuple[str, str]:
"""
Expand Down Expand Up @@ -354,7 +362,10 @@ def get_db_object(self, session: Session) -> File:
"""
Override DownloadJob.
"""
return session.query(File).filter_by(uuid=self.uuid).one()
try:
return session.query(File).filter_by(uuid=self.uuid).one()
except NoResultFound:
raise DownloadException("File not found in database", File, self.uuid)

def call_download_api(self, api: API, db_object: File) -> tuple[str, str]:
"""
Expand Down