Skip to content

Commit

Permalink
avro: also improve TypeError handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Juarez Rudsatz authored and juarezr committed Aug 6, 2020
1 parent 7a1b783 commit d713928
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
29 changes: 18 additions & 11 deletions petl/io/avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,12 @@ def _write_toavro(table, target, mode, schema, sample,
try:
writer.write(record)
num = num + 1
except ValueError as err:
details = _get_error_details(target, num, err, record, schema)
_raise_error(details)
except ValueError as verr:
vmsg = _get_error_details(target, num, verr, record, schema)
_raise_error(ValueError, vmsg)
except TypeError as terr:
tmsg = _get_error_details(target, num, terr, record, schema)
_raise_error(TypeError, tmsg)
# finish writing
writer.flush()

Expand Down Expand Up @@ -541,14 +544,18 @@ def _get_schema_header_names(schema):
return header


def _raise_error(details):
if PY3:
raise ValueError(details).with_traceback(sys.exc_info()[2])
else:
import traceback
stacktrace = traceback.format_exc(sys.exc_info())
err = "%s%s" % (stacktrace, details)
raise ValueError(err)
def _raise_error(ErrorType, new_message):
"""Works like raise Excetion(msg) from prev_exp in python3."""
exinf = sys.exc_info()
tracebk = exinf[2]
try:
if PY3:
raise ErrorType(new_message).with_traceback(tracebk)
# Python2 compatibility workaround
exec('raise ErrorType, new_message, tracebk')
finally:
exinf = None
tracebk = None # noqa: F841


def _ordered_dict_iterator(table):
Expand Down
23 changes: 18 additions & 5 deletions petl/test/io/test_avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,27 @@ def test_appendavro22():
def test_appendavro10():
_append_to_avro_file(table11, table12, schema1)

def test_toavro_troubleshooting():
wrong_schema = dict(schema0)
schema_fields = wrong_schema['fields']
def test_toavro_troubleshooting10():
nullable_schema = dict(schema0)
schema_fields = nullable_schema['fields']
for field in schema_fields:
field['type'] = ['null', 'string']
try:
_write_temp_avro_file(table1, wrong_schema)
except ValueError:
_write_temp_avro_file(table1, nullable_schema)
except ValueError as vex:
bob = "%s" % vex
assert 'Bob' in bob
return
assert False, 'Failed schema conversion'

def test_toavro_troubleshooting11():
table0 = list(table1)
table0[3][1] = None
try:
_write_temp_avro_file(table0, schema1)
except TypeError as tex:
joe = "%s" % tex
assert 'Joe' in joe
return
assert False, 'Failed schema conversion'

Expand Down

0 comments on commit d713928

Please sign in to comment.