mirror of https://github.com/scrapy/scrapy.git
improved errors of ItemLoader.load_item() so that it shows the field name and value of the output processor that failed
This commit is contained in:
parent
fbafb295e8
commit
d97a9d8731
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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'])
|
||||
|
|
|
|||
Loading…
Reference in New Issue