Scrapy, a fast high-level web crawling & scraping framework for Python.
Mention the item in feed export errors (#8191)
Adrian2a6f25272556
diff --git a/scrapy/extensions/feedexport.py b/scrapy/extensions/feedexport.pyindex f1d0d980a..30c131d86 100644--- a/scrapy/extensions/feedexport.py+++ b/scrapy/extensions/feedexport.py@@ -695,7 +695,12 @@ class FeedExporter:slot.start_exporting()assert slot.exporter- slot.exporter.export_item(item)+ try:+ slot.exporter.export_item(item)+ except Exception as e:+ if sys.version_info >= (3, 11):+ e.add_note(f"Item: {item!r}")+ raiseslot.itemcount += 1# create new slot for each slot with itemcount == FEED_EXPORT_BATCH_ITEM_COUNT and close the old oneif (diff --git a/tests/test_feedexport.py b/tests/test_feedexport.pyindex 08ba230f8..306b12ae3 100644--- a/tests/test_feedexport.py+++ b/tests/test_feedexport.py@@ -5,6 +5,7 @@ import jsonimport loggingimport marshalimport pickle+import sysimport tempfilefrom logging import getLoggerfrom pathlib import Path@@ -527,6 +528,25 @@ class TestFeedExport(TestFeedExportBase):assert not listener.start_without_finishassert not listener.finish_without_start+ @pytest.mark.skipif(+ sys.version_info < (3, 11), reason="BaseException.add_note() is 3.11+"+ )+ @coroutine_test+ async def test_export_item_exception_mentions_item(+ self, caplog: pytest.LogCaptureFixture+ ):+ items = [{"foo": {None: "bar"}}]+ settings = {+ "FEEDS": {+ self._random_temp_filename(): {"format": "json"},+ },+ "FEED_EXPORTERS": {"json": ExceptionJsonItemExporter},+ }+ with caplog.at_level(logging.ERROR):+ await self.exported_data(items, settings)+ assert "RuntimeError: foo" in caplog.text+ assert "Item: {'foo': {None: 'bar'}}" in caplog.text+@coroutine_testasync def test_start_finish_exporting_no_items_exception(self):items: list[Any] = []