How it works...

When an exception is raised in Python, it propagates up the call stack until it is processed. In Odoo, the RPC layer that answers the calls made by the web client catches all exceptions and, depending on the exception class, triggers different possible behaviors on the web client.

Any exception not defined in odoo.exceptions will be handled as an Internal Server Error (HTTP status 500) with the stack trace. A UserError will display an error message in the user interface. The code of the recipe raises UserError to ensure that the message is displayed in a user-friendly way. In all cases, the current database transaction is rolled back.

We are using a function with a strange name, _(), which is defined in odoo.tools.translate. This function is used to mark a string as translatable, and to retrieve the translated string at runtime, given the language of the end user that's found in the execution context. More information on this is available in Chapter 12, Internationalization.

When using the _() function, ensure that you pass only strings with the interpolation placeholder, not the whole interpolated string. Example for, _('Warning: could not find %s') % value is correct, but _('Warning: could not find %s' % value) is incorrect because the first one will not find the string with the substituted value in the translation database.
..................Content has been hidden....................

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