4
votes

Warning:

warning = {
                    'title': _('Warning!'),
                    'message': _('Exists the discount limit'),
                }
            return {'warning': warning}

UserError

raise UserError(_('Exists the discount limit'))

What is the difference between Warning and UserError in odoo

3

3 Answers

2
votes

To answer it in simple manner, both are same in odoo. Because if you check in odoo/exception.py refer odoo exception here

It clearly understood that it is referring Warning as UserError. odoo warning is deprecated in odoo v9 and v10 due to ambiguity or collision with python built-in.

To get the information about python warning refer python warning documentation.

So it is recommended to use odoo.exceptions.UserError instead of Warning

2
votes

when an exception is raised in python. it propagates up the call stack until it is processed on 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 web client.

An UserError will display error message in the user interface its define in odoo.exceptions class. The code of recipe changes the OSError to UserError to ensure the message is displayed in a friendly way. In all cases, the current database transaction is rolled back. In Odoo 9 and 10 use UserError to display error message.

Warning: in Odoo 8 openerp.exception.Warning played the role of UserError in 9 and 10 its deprecated because the name was deceptive (it is an error not warning) it collided with python built-in Warning class, it is kept for backward compatibility and you should use UserError in odoo 9 and 10.

in your case while we return a warning it means its only shows the warning message to user and transaction will not rollback.

2
votes

A warning is a python keyword and UserError is used in Odoo, and in odoo warning is deprecated in odoo v9 an onwards.

SO We used UserError in Odoo.