This question has been flagged
1 Reply
3674 Views

Hi All,


I want to inherit the _handle_exception method from http.py file and want to rename Odoo Server Error to Custom Server Error.

class JsonRequest(WebRequest):
def _handle_exception(self, exception):
"""Called within an except block to allow converting exceptions
to arbitrary responses. Anything returned (except None) will
be used as response."""
try:
return super(JsonRequest, self)._handle_exception(exception)
except Exception:
if not isinstance(exception, (odoo.exceptions.Warning, SessionExpiredException,
odoo.exceptions.except_orm, werkzeug.exceptions.NotFound)):
_logger.exception("Exception during JSON request handling.")
error = {
'code': 200,
'message': "Odoo Server Error",
'data': serialize_exception(exception)
}
if isinstance(exception, werkzeug.exceptions.NotFound):
error['http_status'] = 404
error['code'] = 404
error['message'] = "404: Not Found"
if isinstance(exception, AuthenticationError):
error['code'] = 100
error['message'] = "Odoo Session Invalid"
if isinstance(exception, SessionExpiredException):
error['code'] = 100
error['message'] = "Odoo Session Expired"
return self._json_response(error=error)
In the above method, i want to replace the naem from doo to custom name.

Thanks in advance.
Avatar
Discard
Best Answer

Hi Gokulakrishnan

I came across your question, in searching for the exact thing myself for over a year like yourself.  However, as God would have it, I found the solution and I'll post it below for you and anyone else who may be interested:

Create a new controller file and don't forget to place it in the __init__.py file in the controllers directory.  You could always place it in an existing controller file, but a separate file is cleaner imho.

your_controller_file.py

# -*- coding: utf-8 -*-

import werkzeug
import logging
import odoo
import odoo.exceptions
import werkzeug.exceptions
import traceback
from odoo.http import JsonRequest, AuthenticationError, SessionExpiredException, ustr
_logger = logging.getLogger(__name__)

def _handle_exception(self, exception):
"""Called within an except block to allow converting exceptions
to arbitrary responses. Anything returned (except None) will
be used as response."""
try:
return super(JsonRequest, self)._handle_exception(exception)
except Exception:
if not isinstance(exception, (odoo.exceptions.Warning, SessionExpiredException,
odoo.exceptions.except_orm, werkzeug.exceptions.NotFound)):
_logger.exception("Exception during JSON request handling.")
error = {
'code': 200,
'message': "Custom Name Server Error",
'data': serialize_exception(exception)
}
if isinstance(exception, werkzeug.exceptions.NotFound):
error['http_status'] = 404
error['code'] = 404
error['message'] = "404: Not Found"
if isinstance(exception, AuthenticationError):
error['code'] = 100
error['message'] = "Custom Name Session Invalid"
if isinstance(exception, SessionExpiredException):
error['code'] = 100
error['message'] = "Custom Name Session Expired"
return self._json_response(error=error)

setattr(JsonRequest, '_handle_exception', _handle_exception)

def serialize_exception(e):
tmp = {
"name": type(e).__module__ + "." + type(e).__name__ if type(e).__module__ else type(e).__name__,
"debug": traceback.format_exc(),
"message": ustr(e),
"arguments": e.args,
"exception_type": "internal_error"
}
if isinstance(e, odoo.exceptions.UserError):
tmp["exception_type"] = "user_error"
elif isinstance(e, odoo.exceptions.Warning):
tmp["exception_type"] = "warning"
elif isinstance(e, odoo.exceptions.RedirectWarning):
tmp["exception_type"] = "warning"
elif isinstance(e, odoo.exceptions.AccessError):
tmp["exception_type"] = "access_error"
elif isinstance(e, odoo.exceptions.MissingError):
tmp["exception_type"] = "missing_error"
elif isinstance(e, odoo.exceptions.AccessDenied):
tmp["exception_type"] = "access_denied"
elif isinstance(e, odoo.exceptions.ValidationError):
tmp["exception_type"] = "validation_error"
elif isinstance(e, odoo.exceptions.except_orm):
tmp["exception_type"] = "except_orm"
return tmp

Adjust the above parts where it says "Custom Name" to your liking, save, restart the service/server and you're good to go.

The "setattr(JsonRequest, '_handle_exception', _handle_exception)" line above, is the key line, as this is where the override takes place.  Answer inspired from https://stackoverflow.com/questions/62388254/in-odoos-controller-file-how-to-change-the-json-response-format-when-the-type

Avatar
Discard