fix(api): stop swallowing document indexing errors in create handler (#38192)

Co-authored-by: Harsh Kashyap <Harsh23Kashyap@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Harsh Kashyap <harshkashyap@Harshs-MacBook-Pro.local>
Co-authored-by: yunlu.wen <yunlu.wen@dify.ai>
This commit is contained in:
Harsh Kashyap 2026-07-02 11:59:56 +05:30 committed by GitHub
parent d9884efaee
commit 31bb8abbf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 99 additions and 9 deletions

View File

@ -1,4 +1,3 @@
import contextlib
import logging
import time
@ -42,11 +41,12 @@ def handle(sender, **kwargs):
session.add(document)
session.commit()
with contextlib.suppress(Exception):
try:
indexing_runner = IndexingRunner()
indexing_runner.run(documents)
end_at = time.perf_counter()
logger.info(click.style(f"Processed dataset: {dataset_id} latency: {end_at - start_at}", fg="green"))
except DocumentIsPausedError as ex:
logger.info(click.style(str(ex), fg="yellow"))
try:
indexing_runner = IndexingRunner()
indexing_runner.run(documents)
end_at = time.perf_counter()
logger.info(click.style(f"Processed dataset: {dataset_id} latency: {end_at - start_at}", fg="green"))
except DocumentIsPausedError as ex:
logger.info(click.style(str(ex), fg="yellow"))
except Exception:
logger.exception("Document index event handler failed, dataset_id: %s", dataset_id)

View File

@ -0,0 +1,90 @@
import logging
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from core.indexing_runner import DocumentIsPausedError
from events.event_handlers import create_document_index as handler_module
@pytest.fixture
def mock_document() -> SimpleNamespace:
return SimpleNamespace(
id="doc-1",
dataset_id="dataset-1",
indexing_status=None,
processing_started_at=None,
)
@pytest.fixture
def mock_session(mock_document: SimpleNamespace) -> MagicMock:
session = MagicMock()
session.scalar.return_value = mock_document
return session
@pytest.fixture
def mock_session_factory(mock_session: MagicMock) -> MagicMock:
factory = MagicMock()
factory.create_session.return_value.__enter__.return_value = mock_session
factory.create_session.return_value.__exit__.return_value = None
return factory
@pytest.fixture
def mock_indexing_runner() -> MagicMock:
return MagicMock()
def test_handle_logs_document_pause(
mock_session_factory: MagicMock,
mock_indexing_runner: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
mock_indexing_runner.run.side_effect = DocumentIsPausedError("Document is paused")
with (
patch.object(handler_module, "session_factory", mock_session_factory),
patch.object(handler_module, "IndexingRunner", return_value=mock_indexing_runner),
):
with caplog.at_level(logging.INFO, logger=handler_module.logger.name):
handler_module.handle("dataset-1", document_ids=["doc-1"])
assert "Document is paused" in caplog.text
def test_handle_logs_unexpected_indexing_errors(
mock_session_factory: MagicMock,
mock_indexing_runner: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
mock_indexing_runner.run.side_effect = RuntimeError("Indexing failed")
with (
patch.object(handler_module, "session_factory", mock_session_factory),
patch.object(handler_module, "IndexingRunner", return_value=mock_indexing_runner),
):
with caplog.at_level(logging.ERROR, logger=handler_module.logger.name):
handler_module.handle("dataset-1", document_ids=["doc-1"])
assert any(record.levelno >= logging.ERROR for record in caplog.records)
assert "Document index event handler failed" in caplog.text
assert "Indexing failed" in caplog.text
def test_handle_runs_indexing_on_success(
mock_session_factory: MagicMock,
mock_indexing_runner: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
with (
patch.object(handler_module, "session_factory", mock_session_factory),
patch.object(handler_module, "IndexingRunner", return_value=mock_indexing_runner),
):
with caplog.at_level(logging.INFO, logger=handler_module.logger.name):
handler_module.handle("dataset-1", document_ids=["doc-1"])
mock_indexing_runner.run.assert_called_once()
assert "Processed dataset: dataset-1" in caplog.text