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, it will trigger 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 changes the OSError to a UserError to ensure that the message is displayed in a friendly way. In all cases, the current database transaction is rolled back.

Of course, it is not required to catch an exception with a try..except, construct to raise a UserError exception. It is perfectly okay to test for some condition, such as the presence of illegal characters in a filename, and to raise the exception when that test is True. This will prevent further processing of the user request.

We are using a function with a strange name, _(), 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 found in the execution context. More information is available in Chapter 12, Internationalization.

When using the _() function, ensure that you pass it the string with the interpolation placeholder, not the interpolated string:
 _('Warning: could not find %s') % value
is correct, but
_('Warning: could not find %s' % value)
is not.
This last version will certainly 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
18.227.111.33