Raising Exceptions in Odoo 13

Raising Exceptions in Odoo 13

In certain situations, there will be a need to prevent the program from continuing its execution. We can do this by showing up an error message by raising exceptions. In addition to the exceptions available in python, Odoo provides a few more exceptions which include:
    1. access denied
    2. access error
    3. cache miss
    4. missing error
    5. redirect warning
    6. user error 
    7. validation error

When an exception occurs all the data manipulations that happened till then in the database get rolled back. Hence all these operations get canceled and thus prevent incorrect data entry into the database.
Access denied: in the cases where access has to be denied as the data obtained is not the required one, we can use this exception. For example, when someone tries to login with incorrect login details the exception will be raised.

from odoo import models,_
from odoo.exceptions import AccessDenied
class TestingExceptions(models.Model):
    _name = 'testing.exceptions'
    def test(self):
        raise AccessDenied(_("Too many login failures, please wait a bit before trying again."))

raising-exceptions-odoo-13-cybrosys

Access error: when access to data is restricted. For example, trying to read or update records that are not allowed to be accessed by the current user.
from odoo import models,_
from odoo.exceptions import AccessError
 
class TestingExceptions(models.Model):
    _name = 'testing.exceptions'
 
    def test(self):
        raise AccessError(_('The requested operation cannot be completed due to security restrictions.'))

raising-exceptions-odoo-13-cybrosys

Cache miss: when the cache is cleared and you try to access a record in it, then the value in cache is missing and we can use this exception in such a case.
def test_get(self, record, field):
       raise CacheMiss(record, field)

Missing error: when the record that you are searching for getting deleted and you try to write values into the deleted record, it happens to be a missing record case.
from odoo import models,_
from odoo.exceptions import MissingError
class TestingExceptions(models.Model):
    _name = 'testing.exceptions'
    def test(self):
        raise MissingError(_("Record does not exist or has been deleted."))

raising-exceptions-odoo-13-cybrosys

Redirect warning: instead of just showing an exception warning, this one allows you to redirect the user to a given path. For this, we have to provide two parameters in the exception:action_id(the id of the corresponding action where the exception should redirect the user to), button_text (the text on the redirect button).

from odoo import models,_
from odoo.exceptions import RedirectWarning
class TestingExceptions(models.Model):
    _name = 'testing.exceptions'
    def test(self):
        action = self.env.ref('account.action_account_config')
    msg = _('Cannot find a chart of accounts for this company.nPlease go to Account Configuration.')
    raise RedirectWarning(msg, action.id, _('Go to the configuration panel'))

raising-exceptions-odoo-13-cybrosys

User error: when the user tries to make an update on a record that doesn’t seem to suit the current context of that record.
from odoo import models,_
from odoo.exceptions import UserError
class TestingExceptions(models.Model):
    _name = 'testing.exceptions'
    def test(self):
        raise UserError(_('You should provide a lot/serial number for a component'))

raising-exceptions-odoo-13-cybrosys

Validation error: when constraints that have effect on the given record fails. For example, trying to create a new user login that already exists in the same database.
from odoo import models,_
from odoo.exceptions import ValidationError
class TestingExceptions(models.Model):
    _name = 'testing.exceptions'
    def test(self):
        raise ValidationError(
   _('The default Unit of Measure and the purchased Unit of Measure must be in the same category.'))

raising-exceptions-odoo-13-cybrosys

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *