Skip to content
Merged
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
13 changes: 12 additions & 1 deletion packages/dsw-document-worker/dsw/document_worker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ def db_message(self):
f'{str(self.exc)}'


def create_job_exception(job_id: str, message: str, exc=None):
class DocumentNotFoundException(JobException):
pass


def create_job_exception(job_id: str, message: str, document_found=True, exc=None):
if not document_found:
return DocumentNotFoundException(
job_id=job_id,
msg=message,
exc=exc,
)

if isinstance(exc, JobException):
return exc

Expand Down
26 changes: 25 additions & 1 deletion packages/dsw-document-worker/dsw/document_worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
CMD_COMPONENT, CMD_CHANNEL, PROG_NAME
from .context import Context
from .documents import DocumentFile, DocumentNameGiver
from .exceptions import create_job_exception, JobException
from .exceptions import create_job_exception, JobException, \
DocumentNotFoundException
from .limits import LimitsEnforcer
from .templates import TemplateRegistry, Template, Format
from .utils import byte_size_format, check_metamodel_version
Expand Down Expand Up @@ -84,6 +85,21 @@ def safe_format(self) -> Format:
raise RuntimeError('Format is not set but it should')
return self.format

def check_document_exists(self) -> bool:
LOG.debug('Checking if document "%s" exists in DB', self.doc_uuid)
try:
doc = self.ctx.app.db.fetch_document(
document_uuid=self.doc_uuid,
tenant_uuid=self.tenant_uuid,
)
exists = doc is not None
LOG.debug('Document "%s" exists: %s', self.doc_uuid, exists)
return exists
except Exception as e:
LOG.error('Failed to check if document "%s" exists: %s',
self.doc_uuid, str(e))
return False
Comment on lines +88 to +101
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check_document_exists() method duplicates the document fetching logic from get_document() (lines 113-116). Both methods call fetch_document with the same parameters. This duplication could lead to maintenance issues if the fetching logic needs to change. Consider refactoring to have a single method that performs the fetch, or have this method leverage the existing get_document() method's fetch logic.

Copilot uses AI. Check for mistakes.

@handle_job_step('Failed to get document from DB')
def get_document(self):
SentryReporter.set_tags(phase='fetch')
Expand All @@ -102,6 +118,7 @@ def get_document(self):
raise create_job_exception(
job_id=self.doc_uuid,
message='Document record not found in database',
document_found=False,
)
self.doc.retrieved_at = datetime.datetime.now(tz=datetime.UTC)
LOG.info('Job "%s" details received', self.doc_uuid)
Expand Down Expand Up @@ -312,6 +329,11 @@ def _run(self):
self.finalize()

def _set_failed(self, message: str):
document_exists = self.check_document_exists()
if not document_exists:
LOG.warning('Document %s does not exist, cannot set to FAILED',
self.doc_uuid)
return
Comment on lines +334 to +336
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check_document_exists() method returns False on any exception (line 98-101), which could hide database connectivity or permission issues. When database errors occur, the code will incorrectly assume the document doesn't exist and silently return without setting the FAILED state or reporting the actual error. Consider distinguishing between 'document not found' and 'error checking document existence' to avoid masking genuine database problems.

Suggested change
LOG.warning('Document %s does not exist, cannot set to FAILED',
self.doc_uuid)
return
LOG.warning(
'Document %s may not exist or could not be checked; '
'attempting to set state to FAILED anyway',
self.doc_uuid,
)

Copilot uses AI. Check for mistakes.
if self.try_set_job_state(DocumentState.FAILED, message):
LOG.info('Set state to FAILED')
else:
Expand All @@ -323,6 +345,8 @@ def _set_failed(self, message: str):
def run(self):
try:
self._run()
except DocumentNotFoundException as e:
LOG.warning('Document not found: %s', e.log_message())
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DocumentNotFoundException is caught and logged, but no error reporting to Sentry occurs. This creates inconsistent error reporting compared to the JobException handler (lines 350-353) which captures the exception in Sentry. Add SentryReporter.capture_exception(e) to ensure these errors are properly tracked.

Suggested change
LOG.warning('Document not found: %s', e.log_message())
LOG.warning('Document not found: %s', e.log_message())
SentryReporter.capture_exception(e)

Copilot uses AI. Check for mistakes.
except JobException as e:
LOG.warning('Handled job error: %s', e.log_message())
SentryReporter.capture_exception(e)
Expand Down
Loading