From 24f6766bb741b6559220725a3f45a1bfa178993d Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 23 Jun 2025 09:55:21 -0700 Subject: [PATCH] Fix the current handling --- src/documents/consumer.py | 23 +++++++++++++++-------- src/documents/tests/test_consumer.py | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 20ce22e3b..e25f438db 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -468,6 +468,7 @@ class ConsumerPlugin( # now that everything is done, we can start to store the document # in the system. This will be a transaction and reasonably fast. success = False + result = None try: with transaction.atomic(): # store the document. @@ -535,13 +536,17 @@ class ConsumerPlugin( success = True except Exception as e: - self._fail( - str(e), - f"The following error occurred while storing document " - f"{self.filename} after parsing: {e}", - exc_info=True, - exception=e, - ) + # save the exception for later + try: + self._fail( + str(e), + f"The following error occurred while storing document " + f"{self.filename} after parsing: {e}", + exc_info=True, + exception=e, + ) + except Exception as fail_exc: + stored_exception = fail_exc finally: if success: # Delete the file only if it was successfully consumed @@ -577,10 +582,12 @@ class ConsumerPlugin( document.refresh_from_db() result = f"Success. New document id {document.pk} created" + elif stored_exception: + raise stored_exception else: self._fail( ConsumerStatusShortMessage.FAILED, - f"Document {self.filename} was not saved.", + f"Error occurred while saving {self.filename}.", ) document_parser.cleanup() diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index f0fdc02c7..6f1944fc0 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -633,6 +633,32 @@ class TestConsumer( # Database empty self.assertEqual(Document.objects.all().count(), 0) + @mock.patch("documents.consumer.ConsumerPlugin._store") + @mock.patch("documents.consumer.document_consumption_finished.send") + @mock.patch("documents.consumer.generate_unique_filename") + def test_post_consume_fails_silently(self, m_filename, m_signal, m_store): + """ + If _store() returns None but no exception is raised, _fail should still be called. + """ + filename = self.get_test_file() + + # Make _store() return None + m_store.return_value = None + + # Cause crash in a predictable, testable place + m_filename.side_effect = AttributeError("BOOM") + + with self.get_consumer(filename) as consumer: + with self.assertRaisesMessage( + ConsumerError, + "sample.pdf: The following error occurred while storing document sample.pdf after parsing: BOOM", + ): + consumer.run() + + self._assert_first_last_send_progress(last_status="FAILED") + self.assertIsFile(filename) + self.assertEqual(Document.objects.count(), 0) + @override_settings(FILENAME_FORMAT="{correspondent}/{title}") def testFilenameHandling(self): with self.get_consumer(