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

View File

@ -636,9 +636,16 @@ class TestConsumer(
@mock.patch("documents.consumer.ConsumerPlugin._store") @mock.patch("documents.consumer.ConsumerPlugin._store")
@mock.patch("documents.consumer.document_consumption_finished.send") @mock.patch("documents.consumer.document_consumption_finished.send")
@mock.patch("documents.consumer.generate_unique_filename") @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() filename = self.get_test_file()
# Make _store() return None
m_store.return_value = None m_store.return_value = None
# Cause crash in a predictable, testable place
m_filename.side_effect = AttributeError("BOOM") m_filename.side_effect = AttributeError("BOOM")
with self.get_consumer(filename) as consumer: with self.get_consumer(filename) as consumer: