# More user-friendly handling of simpleeval errors

**URL:** https://discuss.tryton.org/t/more-user-friendly-handling-of-simpleeval-errors/4027
**Category:** Ideas
**Created:** [April 9, 2021, 4:02pm UTC](https://discuss.tryton.org/t/more-user-friendly-handling-of-simpleeval-errors/4027 "2021-04-09T16:02:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [April 9, 2021, 4:02pm UTC](https://discuss.tryton.org/t/more-user-friendly-handling-of-simpleeval-errors/4027/1 "2021-04-09T16:02:47Z")

</div>

The account module uses simpleeval to allow custom move templates.

When an error occurs in one of thos expressions the whole traceback is shown.

For example:

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/simpleeval.py", line 431, in _eval_name
    return self.names[node.id]
KeyError: 'importesS10'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tryton/trytond/trytond/protocols/dispatcher.py", line 186, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/tryton/trytond/trytond/wizard/wizard.py", line 290, in execute
    return wizard._execute(state_name)
  File "/tryton/trytond/trytond/wizard/wizard.py", line 321, in _execute
    result = self._execute(transition())
  File "/tryton/trytond/trytond/wizard/wizard.py", line 316, in _execute
    do_result = do(action)
  File "/tryton/trytond/trytond/modules/account/move_template.py", line 330, in do_open_
    move = self.create_move()
  File "/tryton/trytond/trytond/modules/account/move_template.py", line 303, in create_move
    move = template.get_move(values)
  File "/tryton/trytond/trytond/modules/account/move_template.py", line 58, in get_move
    move.lines = [l.get_line(values) for l in self.lines]
  File "/tryton/trytond/trytond/modules/account/move_template.py", line 58, in <listcomp>
    move.lines = [l.get_line(values) for l in self.lines]
  File "/tryton/trytond/trytond/modules/analytic_account_move/move.py", line 139, in get_line
    line = super(MoveLineTemplate, self).get_line(values)
  File "/tryton/trytond/trytond/modules/account/move_template.py", line 194, in get_line
    functions={'Decimal': Decimal}, names=values)
  File "/usr/local/lib/python3.7/dist-packages/simpleeval.py", line 609, in simple_eval
    return s.eval(expr)
  File "/usr/local/lib/python3.7/dist-packages/simpleeval.py", line 332, in eval
    return self._eval(ast.parse(expr.strip()).body[0].value)
  File "/usr/local/lib/python3.7/dist-packages/simpleeval.py", line 343, in _eval
    return handler(node)
  File "/usr/local/lib/python3.7/dist-packages/simpleeval.py", line 443, in _eval_name
    raise NameNotDefined(node.id, self.expr)
simpleeval.NameNotDefined: 'importesS10' is not defined for expression 'importesS10 '

```

I propose we capture simpleeval expressions and raise a UserError with just the relevant error.

Something like: “The following error was found when evaluating expression: ‘importesS10’ is not defined for expression 'importesS10 '.”

Thoughts?

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [April 9, 2021, 7:07pm UTC](https://discuss.tryton.org/t/more-user-friendly-handling-of-simpleeval-errors/4027/2 "2021-04-09T19:07:58Z")

</div>

Makes sense. This something that some user should fix so we should show and undestanable message to them. Furthermore, your proposal will be in concordance with the [modify cost price wizard](http://hg.tryton.org/modules/stock/file/31d952c5014c/product.py#l1131)

Feel free to [submit a patch](https://www.tryton.org/develop)

---

<div class="post-metadata">

### Author: ![juanjogp](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/c68b51/32.png) [@juanjogp](https://discuss.tryton.org/u/juanjogp)
#### Post date: [November 30, 2021, 2:06pm UTC](https://discuss.tryton.org/t/more-user-friendly-handling-of-simpleeval-errors/4027/3 "2021-11-30T14:06:33Z")

</div>

Hello,  
The patch adding this functionality can be found here: [More user-friendly handling of simpleeval errors (#11000) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue11000)
