Fix the current handling

This commit is contained in:
shamoon 2025-06-23 09:55:21 -07:00
parent f819d8025c
commit 24f6766bb7
No known key found for this signature in database
2 changed files with 41 additions and 8 deletions

View File

@ -468,6 +468,7 @@ class ConsumerPlugin(
# now that everything is done, we can start to store the document # now that everything is done, we can start to store the document
# in the system. This will be a transaction and reasonably fast. # in the system. This will be a transaction and reasonably fast.
success = False success = False
result = None
try: try:
with transaction.atomic(): with transaction.atomic():
# store the document. # store the document.
@ -535,13 +536,17 @@ class ConsumerPlugin(
success = True success = True
except Exception as e: except Exception as e:
self._fail( # save the exception for later
str(e), try:
f"The following error occurred while storing document " self._fail(
f"{self.filename} after parsing: {e}", str(e),
exc_info=True, f"The following error occurred while storing document "
exception=e, f"{self.filename} after parsing: {e}",
) exc_info=True,
exception=e,
)
except Exception as fail_exc:
stored_exception = fail_exc
finally: finally:
if success: if success:
# Delete the file only if it was successfully consumed # Delete the file only if it was successfully consumed
@ -577,10 +582,12 @@ class ConsumerPlugin(
document.refresh_from_db() document.refresh_from_db()
result = f"Success. New document id {document.pk} created" result = f"Success. New document id {document.pk} created"
elif stored_exception:
raise stored_exception
else: else:
self._fail( self._fail(
ConsumerStatusShortMessage.FAILED, ConsumerStatusShortMessage.FAILED,
f"Document {self.filename} was not saved.", f"Error occurred while saving {self.filename}.",
) )
document_parser.cleanup() document_parser.cleanup()

View File

@ -633,6 +633,32 @@ class TestConsumer(
# Database empty # Database empty
self.assertEqual(Document.objects.all().count(), 0) 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}") @override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def testFilenameHandling(self): def testFilenameHandling(self):
with self.get_consumer( with self.get_consumer(