Compare commits

..

3 Commits

Author SHA1 Message Date
shamoon
24f6766bb7
Fix the current handling 2025-06-23 09:55:21 -07:00
shamoon
f819d8025c
Try this 2025-06-23 09:34:29 -07:00
shamoon
0f6e9d4343
Enhancement: better try to catch db errors before unlink 2025-06-23 09:19:28 -07:00
2 changed files with 32 additions and 22 deletions

View File

@ -468,7 +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
store_exception = None
result = None
try:
with transaction.atomic():
# store the document.
@ -535,8 +535,18 @@ class ConsumerPlugin(
document.save()
success = True
except Exception as 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:
store_exception = fail_exc
stored_exception = fail_exc
finally:
if success:
# Delete the file only if it was successfully consumed
@ -571,26 +581,19 @@ class ConsumerPlugin(
# Return the most up to date fields
document.refresh_from_db()
document_parser.cleanup()
tempdir.cleanup()
return f"Success. New document id {document.pk} created"
else:
document_parser.cleanup()
tempdir.cleanup()
if store_exception:
self._fail(
str(store_exception),
f"The following error occurred while storing document "
f"{self.filename} after parsing: {store_exception}",
exc_info=True,
exception=store_exception,
)
result = f"Success. New document id {document.pk} created"
elif stored_exception:
raise stored_exception
else:
self._fail(
ConsumerStatusShortMessage.FAILED,
f"Error occurred while saving {self.filename}.",
)
document_parser.cleanup()
tempdir.cleanup()
return result
def _parse_title_placeholders(self, title: str) -> str:
local_added = timezone.localtime(timezone.now())

View File

@ -636,9 +636,16 @@ class TestConsumer(
@mock.patch("documents.consumer.ConsumerPlugin._store")
@mock.patch("documents.consumer.document_consumption_finished.send")
@mock.patch("documents.consumer.generate_unique_filename")
def testSaveFailsStillCaught(self, m_filename, m_signal, m_store):
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: