There's more...

Sometimes, you are working on error-prone code, meaning that the operation you are performing may generate an error. Odoo will catch this error and display a traceback to the user. If you don't want to show a full error log to the user, you can cache the error and raise a custom exception with a meaningful message. In the given example, we are generating UserError from the try...cache block so that instead of showing a full error log, Odoo will now show a warning with a meaningful message:

def post_to_webservice(self, data):
try:
req = requests.post('http://my-test-service.com', data=data, timeout=10)
content = req.json()
except IOError:
error_msg = _("Something went wrong during data submission")
raise UserError(error_msg)
return content

There are a few more exception classes defined in odoo.exceptions, all deriving from the base legacy except_orm exception class. Most of them are only used internally, apart from the following:

  • Warning: In Odoo 8.0, odoo.exceptions.Warning played the role of UserError in 9.0 and later. It is now deprecated because the name was deceptive (it is an error, not a warning) and it collided with the Python built-in Warning class. It is kept for backward compatibility only, and you should use UserError in your code.
  • ValidationError: This exception is raised when a Python constraint on a field is not respected. In Chapter 5, Application Models, refer to the Adding constraint validations to a model recipe for more information.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset