diff --git a/conservancy_beancount/errors.py b/conservancy_beancount/errors.py index 157e151305a761572d26fac8120b1af113f65bfd..c42ac318370249d628530ba7f53a5746d60700b3 100644 --- a/conservancy_beancount/errors.py +++ b/conservancy_beancount/errors.py @@ -67,26 +67,13 @@ class ConfigurationError(Error): class InvalidMetadataError(Error): - def __init__(self, txn, post, key, value=None, source=None): - if value is None: - msg_fmt = "{post.account} missing {key}" - else: - msg_fmt = "{post.account} has invalid {key}: {value}" - super().__init__( - msg_fmt.format(post=post, key=key, value=value), - txn, - source, - ) - - -class InvalidEntityError(InvalidMetadataError): - def __init__(self, txn, post=None, key='entity', value=None, source=None): + def __init__(self, txn, key, value=None, post=None, source=None): if post is None: srcname = 'transaction' else: srcname = post.account if value is None: - msg = "{} missing entity".format(srcname) + msg = "{} missing {}".format(srcname, key) else: - msg = "{} entity malformed: {}".format(srcname, value) - super(InvalidMetadataError, self).__init__(msg, txn, source) + msg = "{} has invalid {}: {}".format(srcname, key, value) + super().__init__(msg, txn, source) diff --git a/conservancy_beancount/plugin/core.py b/conservancy_beancount/plugin/core.py index 6699da99b7d8b13944a4e04ebf0b4e3e93b22245..f0e7f65333900225409fd28d031a654225fe8467 100644 --- a/conservancy_beancount/plugin/core.py +++ b/conservancy_beancount/plugin/core.py @@ -217,7 +217,7 @@ class _NormalizePostingMetadataHook(_PostingHook): # a value string from METADATA_ENUM, or else raise InvalidMetadataError. # This base implementation does the latter. def _default_value(self, txn: Transaction, post: data.Posting) -> MetaValueEnum: - raise errormod.InvalidMetadataError(txn, post, self.METADATA_KEY) + raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter: source_value = post.meta.get(self.METADATA_KEY) @@ -233,7 +233,7 @@ class _NormalizePostingMetadataHook(_PostingHook): set_value = self.VALUES_ENUM[source_value] except KeyError: error = errormod.InvalidMetadataError( - txn, post, self.METADATA_KEY, source_value, + txn, self.METADATA_KEY, source_value, post, ) if error is None: post.meta[self.METADATA_KEY] = set_value diff --git a/conservancy_beancount/plugin/meta_entity.py b/conservancy_beancount/plugin/meta_entity.py index 7fe87d8f99a6ee9f1d550c79a5688d46374d6a44..93fb67cd0eba3ce86692b24f33dfa769590ca7f4 100644 --- a/conservancy_beancount/plugin/meta_entity.py +++ b/conservancy_beancount/plugin/meta_entity.py @@ -37,14 +37,14 @@ class MetaEntity(core.TransactionHook): else: txn_entity_ok = False if txn_entity_ok is False: - yield errormod.InvalidEntityError(txn, value=txn_entity) + yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, txn_entity) for post in data.iter_postings(txn): if post.account.is_under('Assets', 'Liabilities'): continue entity = post.meta.get(self.METADATA_KEY) if entity is None: - yield errormod.InvalidEntityError(txn, post) + yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, entity, post) elif entity is txn_entity: pass elif not self.ENTITY_RE.match(entity): - yield errormod.InvalidEntityError(txn, post, value=entity) + yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, entity, post) diff --git a/conservancy_beancount/plugin/meta_income_type.py b/conservancy_beancount/plugin/meta_income_type.py index fd87b3748074a2cd9c6349a223525eac25a96e06..73038b63d754b7643785b71ea307349c972a563e 100644 --- a/conservancy_beancount/plugin/meta_income_type.py +++ b/conservancy_beancount/plugin/meta_income_type.py @@ -49,4 +49,4 @@ class MetaIncomeType(core._NormalizePostingMetadataHook): try: return self.DEFAULT_VALUES[post.account] except KeyError: - raise errormod.InvalidMetadataError(txn, post, self.METADATA_KEY) from None + raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) from None diff --git a/conservancy_beancount/plugin/meta_project.py b/conservancy_beancount/plugin/meta_project.py index 5194534d015466e4bed66a11b8a937c125cb6b1d..cde952dff5d29634d22ba3ff3ceabdbe4f60f22e 100644 --- a/conservancy_beancount/plugin/meta_project.py +++ b/conservancy_beancount/plugin/meta_project.py @@ -87,5 +87,4 @@ class MetaProject(core._NormalizePostingMetadataHook): ): return self.DEFAULT_PROJECT else: - raise errormod.InvalidMetadataError(txn, post, self.METADATA_KEY) - + raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) diff --git a/tests/test_meta_entity.py b/tests/test_meta_entity.py index c0902d29bf5f0919ac300aae33ce982bd7181198..3a0c6c12f4f7705ca2e36c48cfe09e548a6b6a98 100644 --- a/tests/test_meta_entity.py +++ b/tests/test_meta_entity.py @@ -60,7 +60,7 @@ def test_invalid_values_on_postings(hook, src_value): ]) errors = list(hook.run(txn)) assert len(errors) == 1 - assert errors[0].message == "Expenses:General entity malformed: {}".format(src_value) + assert errors[0].message == "Expenses:General has invalid entity: {}".format(src_value) @pytest.mark.parametrize('src_value', VALID_VALUES) def test_valid_values_on_transactions(hook, src_value): @@ -78,7 +78,7 @@ def test_invalid_values_on_transactions(hook, src_value): ]) errors = list(hook.run(txn)) assert 1 <= len(errors) <= 2 - assert all(error.message == "transaction entity malformed: {}".format(src_value) + assert all(error.message == "transaction has invalid entity: {}".format(src_value) for error in hook.run(txn)) @pytest.mark.parametrize('account,required', [