Skip to content

Commit

Permalink
Added additional information to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Apr 23, 2020
1 parent 9171565 commit cfb6765
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
13 changes: 11 additions & 2 deletions dataflows/base/datastream_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def __init__(self):
self.stats = {}
self.source = None
self.datapackage = None
self.position = None

def __call__(self, source=None):
def __call__(self, source=None, position=None):
if source is None:
source = DataStream()
self.source = source
self.position = position
return self

def process_resource(self, resource: ResourceWrapper):
Expand Down Expand Up @@ -69,7 +71,14 @@ def func():
return func

def _process(self):
datastream = self.source._process()
try:
datastream = self.source._process()
except Exception as exception:
if (not hasattr(exception, 'processorName')):
exception.processorName = self.__class__.__name__
exception.processorObject = self
exception.processorPosition = self.position
raise exception

self.datapackage = Package(descriptor=copy.deepcopy(datastream.dp.descriptor))
self.datapackage = self.process_datapackage(self.datapackage)
Expand Down
4 changes: 2 additions & 2 deletions dataflows/base/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def _preprocess_chain(self):
def _chain(self, ds=None):
from ..helpers import datapackage_processor, rows_processor, row_processor, iterable_loader

for link in self._preprocess_chain():
for position, link in enumerate(self._preprocess_chain(), start=1):
if isinstance(link, Flow):
ds = link._chain(ds)
elif isinstance(link, DataStreamProcessor):
ds = link(ds)
ds = link(ds, position=position)
elif isfunction(link):
sig = signature(link)
params = list(sig.parameters)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,3 +1531,16 @@ def test_force_temporal_format():
'time': datetime.time(8, 10, 4),
}
]]


def test_exception_information():
from dataflows import load
flow = Flow(
load('data/bad-path1.csv'),
load('data/bad-path2.csv'),
)
with pytest.raises(Exception) as excinfo:
data = flow.results()
assert excinfo.value.processorName == 'load'
assert excinfo.value.processorObject.load_source == 'data/bad-path2.csv'
assert excinfo.value.processorPosition == 2

0 comments on commit cfb6765

Please sign in to comment.