İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
2350 Görünümler

Hello everyone, Odoo beginner here!

My controller is working properly, but i want to be able to return different responses than 200 with specific error messages.

I have found this controller parameter "handle_params_access_error" (new in odoo 17) in the Odoo documentation but i cannot figure out how to use it.


from odoo import http
from werkzeug.exceptions import UnprocessableEntity
# from werkzeug.wrappers.response import Response
from odoo.http import Response

class Api_Name(http.Controller):
    @http.route('/route_name', auth='user', type = "json", csrf=False, method=['POST'])
    def index(self,**kw):
        if len(kw["data"]) != 2:
            response = UnprocessableEntity("Invalid length")
​raise response
​return response
[...]
        return response

When testing the above code with Postman, i receive a status 200 with an error message : "message": "422 Unprocessable Entity: Invalid length".

I cannot manage to receive a status 422 instead.


Any help would be greatly appreciated, thank you !


Avatar
Vazgeç
En İyi Yanıt

handle_params_access_error is the name of a custom method that you want called to handle any error that occurred when retrieving the record.


You can see how we use it in the website_slides module.


In  this module, we handle the case "if a User encounters an access error, we want to redirect them back to /slides with a 302 error".


We do this by: 

1. defining the custom method:


2. using the parameter in the route or routes we want it to be triggered:



Result when trying to access a slide without the right permissions:


See also https://github.com/odoo/odoo/blob/17.0/addons/website_slides/controllers/main.py


Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
7
Ara 24
7021
0
Tem 24
956
0
May 24
1286
0
Nis 24
23
0
Mar 24
1521