diff --git a/cardutil/iso8583.py b/cardutil/iso8583.py index b3c334f..cb1388d 100644 --- a/cardutil/iso8583.py +++ b/cardutil/iso8583.py @@ -188,6 +188,13 @@ def _iso8583_to_dict(message, bit_config, encoding=DEFAULT_ENCODING, hex_bitmap= for bit in range(2, 128): if bitmap_list[bit]: LOGGER.debug("processing bit %s", bit) + # Check that config is available for this bit + if not bit_config.get(str(bit)): + raise Iso8583DataError( + f'No bit config available for bit {bit}', + binary_context_data=message + ) + return_message, message_increment = _iso8583_to_field( bit, bit_config[str(bit)], diff --git a/cardutil/mciipm.py b/cardutil/mciipm.py index 2426322..b24f16c 100644 --- a/cardutil/mciipm.py +++ b/cardutil/mciipm.py @@ -277,7 +277,7 @@ def __next__(self) -> bytes: if record_length < 0 or record_length > 3000: raise MciIpmDataError(f"Invalid record length - value read was {record_length}", record_number=self.record_number, - binary_context_data=self.last_record) + binary_context_data=record_length_raw) # exit if last record (length=0) if record_length == 0: @@ -341,7 +341,7 @@ def __next__(self) -> dict: output = iso8583.loads(vbs_record, encoding=self.encoding, iso_config=self.iso_config) except CardutilError as ex: raise MciIpmDataError( - 'Error while loading ISO8583 record', + 'Error while processing ISO8583 record', binary_context_data=self.last_record, record_number=self.record_number, original_exception=ex diff --git a/tests/cli/test_mci_ipm_to_csv.py b/tests/cli/test_mci_ipm_to_csv.py index 86041a6..0d525a0 100644 --- a/tests/cli/test_mci_ipm_to_csv.py +++ b/tests/cli/test_mci_ipm_to_csv.py @@ -107,7 +107,7 @@ def test_ipm_to_csv_generate_exception(self): os.remove(in_ipm_name) os.remove(in_ipm_name + '.csv') print(output) - assert len(output) == 9 + assert len(output) == 8 assert output[4] == '*** ERROR - processing has stopped ***' diff --git a/tests/test_iso8583.py b/tests/test_iso8583.py index b6753c7..e0364b6 100644 --- a/tests/test_iso8583.py +++ b/tests/test_iso8583.py @@ -193,6 +193,10 @@ def test_iso8583_to_dict(self): _iso8583_to_dict(b'\xFFBCDFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.....', config["bit_config"], encoding="ascii", hex_bitmap=True) + # check exception when bit config not found + with self.assertRaises(CardutilError): + _iso8583_to_dict(message_ascii_raw, {}, "ascii") + def test_dict_to_iso8583(self): source_dict = {'MTI': '1144', 'DE2': '4444555544445555', 'DE3': '111111', 'PDS0001': '1', 'PDS9999': 'Z'} actual_iso = _dict_to_iso8583(source_dict, config['bit_config']) @@ -264,6 +268,9 @@ def test_get_de43_fields(self): self.assertEqual( _get_de43_fields('ALL FIELD', processor_config=custom_processor_config), {'DE43_ALL': 'ALL FIELD'}) + self.assertEqual( + _get_de43_fields('SOME DATA', None), {}) + def test_get_de43_fields_international_addresses(self): default_processor_config = config['bit_config']['43'].get('field_processor_config') # United Kingdom