diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index 762aa6512b..0f79f683eb 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -539,6 +539,7 @@ def rotate( pair = _resolve_root_and_source_doc(doc, source_mode=source_mode) docs_by_root_id.setdefault(pair.root_doc.id, pair) + failed_ids = [] for pair in docs_by_root_id.values(): if pair.source_doc.mime_type != "application/pdf": logger.warning( @@ -573,8 +574,13 @@ def rotate( f"Queued new rotated version for document {pair.root_doc.id} by {degrees} degrees", ) except Exception as e: + failed_ids.append(pair.root_doc.id) logger.exception(f"Error rotating document {pair.root_doc.id}: {e}") - raise + + if failed_ids: + raise RuntimeError( + f"Rotation failed for document(s): {failed_ids}. Check logs for details.", + ) return "OK" diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py index f9a8f71d2e..b5918130e3 100644 --- a/src/documents/tests/test_bulk_edit.py +++ b/src/documents/tests/test_bulk_edit.py @@ -1243,13 +1243,68 @@ class TestPDFActions(DirectoriesMixin, TestCase): doc_ids = [self.doc2.id, self.doc3.id] with self.assertLogs("paperless.bulk_edit", level="ERROR") as cm: - with self.assertRaisesMessage(Exception, "Error rotating PDF"): + with self.assertRaisesMessage( + RuntimeError, + f"Rotation failed for document(s): {doc_ids}", + ): bulk_edit.rotate(doc_ids, 90) error_str = cm.output[0] expected_str = "Error rotating document" self.assertIn(expected_str, error_str) + self.assertEqual(mock_rotate_pdf.call_count, 2) mock_consume_delay.assert_not_called() + @mock.patch("documents.tasks.consume_file.apply_async") + @mock.patch("documents.pdf_ops.rotate_pdf") + def test_rotate_continues_after_preparation_failure( + self, + mock_rotate_pdf, + mock_consume, + ) -> None: + def rotate(source, destination, degrees): + if source == self.doc2.source_path: + raise RuntimeError("Invalid PDF") + shutil.copy(source, destination) + + mock_rotate_pdf.side_effect = rotate + with self.assertRaisesMessage( + RuntimeError, + f"Rotation failed for document(s): [{self.doc2.id}]", + ): + bulk_edit.rotate([self.doc2.id, self.doc3.id], 90) + + self.assertEqual(mock_rotate_pdf.call_count, 2) + mock_consume.assert_called_once() + self.assertEqual( + mock_consume.call_args.kwargs["kwargs"]["input_doc"].root_document_id, + self.doc3.id, + ) + + @mock.patch("documents.tasks.consume_file.apply_async") + @mock.patch("documents.pdf_ops.rotate_pdf") + def test_rotate_continues_after_queue_failure( + self, + mock_rotate_pdf, + mock_consume, + ) -> None: + mock_rotate_pdf.side_effect = lambda source, destination, degrees: shutil.copy( + source, + destination, + ) + mock_consume.side_effect = [RuntimeError("Broker unavailable"), None] + with self.assertRaisesMessage( + RuntimeError, + f"Rotation failed for document(s): [{self.doc2.id}]", + ): + bulk_edit.rotate([self.doc2.id, self.doc3.id], 90) + + self.assertEqual(mock_rotate_pdf.call_count, 2) + self.assertEqual(mock_consume.call_count, 2) + self.assertEqual( + mock_consume.call_args.kwargs["kwargs"]["input_doc"].root_document_id, + self.doc3.id, + ) + @mock.patch("documents.tasks.consume_file.apply_async") def test_rotate_non_pdf( self, diff --git a/src/documents/tests/test_bulk_operations.py b/src/documents/tests/test_bulk_operations.py index be977640c8..1e5a495c2d 100644 --- a/src/documents/tests/test_bulk_operations.py +++ b/src/documents/tests/test_bulk_operations.py @@ -209,7 +209,10 @@ def test_preparation_failure_propagates_from_worker( ), mock.patch("documents.bulk_edit.consume_file.apply_async") as consume, mock.patch("documents.bulk_edit.consume_file.s") as consume_signature, - pytest.raises(RuntimeError, match="preparation failed"), + pytest.raises( + RuntimeError, + match="Rotation failed" if operation == "rotate" else "preparation failed", + ), ): execute_bulk_operation(operation, [doc.pk], parameters, user.pk) consume.assert_not_called()