Enable Dark Mode!
raising-exceptions-in-the-odoo-15.jpg
By: Lajina KV

Raising Exceptions in the Odoo 15

Technical Odoo 15

When something does not work as expected, you may want to inform the user and interrupt the program with an error message. This error message is the Odoo exception. Odoo can provide different types of exceptions.

In this blog, we will discuss exceptions in odoo15. The exceptions are the error messages. These are used to prevent the execution of programs in certain situations for your needs. These exceptions are listed below.

1. ValidationError

2. UserError

3. AccessError

4. MissingError

5. AccessDenied

6. IndirectWarning

7. CacheMiss

1. Validation Error

The Validation Error occurs when the constraints are failed or violated. In the example below,  the user tries to create a record using an email, but another user uses the email. So this exception can be used for the validation process of a record.

from odoo import models, api, _
from odoo.exceptions import ValidationError
class ResPartnerInherited(models.Model):
_inherit = "res.partner"
@api.model
def create(self, vals):
    if self.env["res.partner"].search([("email", "=", vals['email'])]):
        raise ValidationError(_("Another user is already created using this email"))
    return super(ResPartnerInherited, self).create(vals)

raising-exceptions-in-the-odoo-15-cybrosys

2. UserError

A User Error exception is executed when the user is trying to update the records. For example, a user trying to upload an SVG file raises a user error exception, because only an admin can upload files to Odoo

from odoo import models, _
from odoo.exceptions import UserError
class ExceptionHandler(models.Model):
_name = "test.exception"
def test_exception(self):
    raise UserError(_("Only admins can upload SVG files."))

raising-exceptions-in-the-odoo-15-cybrosys

3. AcessError

This Odoo exception is executed while violating the access right of the user. For example, some companies don't have access to update records. So, when unauthorized companies try to update the records, it raises an access error.

from odoo import models, _
from odoo.exceptions import AccessError
class ExceptionHandler(models.Model):
_name = "test.exception"
def test_exception(self):
    raise AccessError(_("Access to unauthorized or invalid companies."))

raising-exceptions-in-the-odoo-15-cybrosys

4. MissingError

The Missing Error is an Odoo exception that will be executed when the user tries to write something in the deleted record.

from odoo import models, _
from odoo.exceptions import MissingError
class ExceptionHandler(models.Model):
_name = "test.exception"
def test_exception(self):
    raise MissingError(_("Record does not exist or has been deleted."))

raising-exceptions-in-the-odoo-15-cybrosys

5. AccessDenied

This exception is used when the user tries to log in using invalid login details. For example, if the user tries to log in many times using invalid credentials, then it will raise an AccessDenied exception.

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

raising-exceptions-in-the-odoo-15-cybrosys

6. RedirectWaring

This exception can be given a particular route for redirecting to a particular page instead of giving a warning message to the user. You should provide  action_id, message, and button text while executing this exception. These are the essential parameters for redirecting warnings.

action_id: It is the id of the action were to perform the redirection

message: It is the message for exception

button_text: It is the text to put on the button that will trigger the redirection

from odoo import models, _
from odoo.exceptions import RedirectWarning
class ExceptionHandler(models.Model):
_name = "test.exception"
def test_exception(self):
    action = self.env.ref('base.action_res_company_form')
    msg = _('Can not create chart of account until you configure your company AFIP Responsibility and VAT.')
    raise RedirectWarning(msg, action.id, _('Go to Companies'))

raising-exceptions-in-the-odoo-15-cybrosys

7. CacheMiss

If some record fields are missing from the cache, then we can use the CacheMiss exception. This exception includes two parameters: a record and a field.

def test_get(self, record, field):  	 
    raise CacheMiss(record, field)

In this way, we can raise exceptions in Odoo to inform the user and stop the program execution if something happens in the expected workflow.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message