Changeset - 4009c626d4cb
[Not reviewed]
0 3 0
Brett Smith - 5 years ago 2019-07-29 16:05:51
brettcsmith@brettcsmith.org
ledger_entry: Improve error reporting for amount expressions.

This is going to become more important as amount expressions become more
capable.
3 files changed with 30 insertions and 5 deletions:
0 comments (0 inline, 0 general)
import2ledger/hooks/ledger_entry.py
Show inline comments
...
 
@@ -170,7 +170,13 @@ class AccountSplitter:
 
        amount_vars['Decimal'] = decimal.Decimal
 
        amounts = [
 
            (account, self._currency_decimal(eval(amount_expr, amount_vars),
 
                                             template_vars['currency']))
 
            for account, amount_expr in self.splits
 
        ]
 
        try:
 
            amounts = [
 
                (account, self._currency_decimal(eval(amount_expr, amount_vars),
 
                                                 template_vars['currency']))
 
                for account, amount_expr in self.splits
 
            ]
 
        except (ArithmeticError, NameError, TypeError, ValueError) as error:
 
            raise errors.UserInputConfigurationError(
 
                "{}: {}".format(type(error).__name__, error),
 
                "template {!r}".format(self.template_name)
 
            ) from error
 
        self._balance_amounts(amounts, template_vars['amount'])
tests/data/templates.ini
Show inline comments
...
 
@@ -55,2 +55,12 @@ template =
 

	
 
[NondecimalWord]
 
template =
 
 Income:Sales  -5
 
 Assets:Cash   foo
 

	
 
[NondecimalVariable]
 
template =
 
 Income:Sales  -5
 
 Assets:Cash   {payee}
 

	
 
[Empty]
tests/test_hook_ledger_entry.py
Show inline comments
...
 
@@ -261 +261,10 @@ def test_hook_handles_template_undefined():
 

	
 
def test_string_value_is_user_error():
 
    entry_data = template_vars('EE', 1)
 
    with pytest.raises(errors.UserInputConfigurationError):
 
        run_hook(entry_data, 'NondecimalWord')
 

	
 
def test_string_variable_is_user_error():
 
    entry_data = template_vars('FF', 1)
 
    with pytest.raises(errors.UserInputConfigurationError):
 
        run_hook(entry_data, 'NondecimalVariable')
0 comments (0 inline, 0 general)