1
votes

I'm defining a server action in Odoo 10. Within this action I am trying to use the following code:

for data in datas:
    try:
        inventory_level = int(data[context['inventory_level_column']].strip())
    except TypeError:
        continue

However, I receive an error:

ValueError: <type 'exceptions.NameError'>: "name 'TypeError' is not defined"

Is it not possible to catch errors within the context of an Odoo server action? Why is TypeError not defined?

2

2 Answers

1
votes

The code written on a server action is passed through to safe_eval method. There the __builtins__ are stripped and replaced (thus the exceptions.NameError class is removed.

You can check this behaviour on odoo/tools/safe_eval.py on the definition of safe_eval method. See globals_dict['__builtins__'] = _BUILTINS where _BUILTINS does not contain this exception.

1
votes

Exception is the parent class of all exception classes, so if you want to catch exception then just specify top most parent class in except and in e you will get error message.

for data in datas:
    try:
        inventory_level = int(data[context['inventory_level_column']].strip())
    except Exception ,e:
        print e
        pass