Scrapy, a fast high-level web crawling & scraping framework for Python.
improved errors of ItemLoader.load_item() so that it shows the field name and value of the output processor that failed
Pablo Hoffmand97a9d87319b
diff --git a/scrapy/contrib/loader/__init__.py b/scrapy/contrib/loader/__init__.pyindex a9fc8f011..fa8387eaa 100644--- a/scrapy/contrib/loader/__init__.py+++ b/scrapy/contrib/loader/__init__.py@@ -79,7 +79,11 @@ class ItemLoader(object):def get_output_value(self, field_name):proc = self.get_output_processor(field_name)proc = wrap_loader_context(proc, self.context)- return proc(self._values[field_name])+ try:+ return proc(self._values[field_name])+ except Exception, e:+ raise ValueError("Error with output processor: field=%r value=%r error='%s: %s'" % \+ (field_name, self._values[field_name], type(e).__name__, str(e)))def get_collected_values(self, field_name):return self._values[field_name]diff --git a/scrapy/tests/test_contrib_loader.py b/scrapy/tests/test_contrib_loader.pyindex 8a730e543..e306b7333 100644--- a/scrapy/tests/test_contrib_loader.py+++ b/scrapy/tests/test_contrib_loader.py@@ -215,6 +215,32 @@ class ItemLoaderTest(unittest.TestCase):il.add_value('name', [u'mar', u'ta'])self.assertEqual(il.get_output_value('name'), u'Mar Ta')++ def test_output_processor_error(self):+ class TestItemLoader(ItemLoader):+ default_item_class = TestItem+ name_out = MapCompose(float)++ il = TestItemLoader()+ il.add_value('name', [u'$10'])+ try:+ float('$10')+ except Exception, e:+ expected_exc_str = str(e)++ exc = None+ try:+ il.load_item()+ except Exception, e:+ exc = e+ assert isinstance(exc, ValueError)+ s = str(exc)+ assert 'name' in s, s+ assert '$10' in s, s+ assert 'ValueError' in s, s+ assert expected_exc_str in s, s++def test_output_processor_using_classes(self):il = TestItemLoader()il.add_value('name', [u'mar', u'ta'])