From 979e4f34d7976c0ab1c3555ed1da5fd7727a4e08 Mon Sep 17 00:00:00 2001 From: emjay0921 Date: Thu, 21 Nov 2024 18:58:25 +0800 Subject: [PATCH] [FIX] spp_api: Override the handle_error function to fix the initialization error --- spp_api/controllers/apijsonrequest.py | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/spp_api/controllers/apijsonrequest.py b/spp_api/controllers/apijsonrequest.py index ba4e1d7e2..7f027d29e 100644 --- a/spp_api/controllers/apijsonrequest.py +++ b/spp_api/controllers/apijsonrequest.py @@ -1,10 +1,15 @@ # Copyright 2021 Denis Mudarisov # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +import collections.abc import logging +import traceback + +from werkzeug.exceptions import NotFound from odoo.http import ( Dispatcher, ) +from odoo.tools import ustr try: import psutil @@ -15,6 +20,23 @@ _logger = logging.getLogger(__name__) +def serialize_exception(exception): + name = type(exception).__name__ + module = type(exception).__module__ + + return { + "name": f"{module}.{name}" if module else name, + "debug": traceback.format_exc(), + "message": ustr(exception), + "arguments": exception.args, + "context": getattr(exception, "context", {}), + } + + +class SessionExpiredException(Exception): + pass + + class ApiJsonRequest(Dispatcher): routing_type = "apijson" @@ -34,3 +56,30 @@ def dispatch(self, endpoint, args): result = endpoint(**self.request.params) return self.request.make_json_response(result.json, status=result.status) + + def handle_error(self, exc: Exception) -> collections.abc.Callable: + """ + Handle any exception that occurred while dispatching a request to + a `type='json'` route. Also handle exceptions that occurred when + no route matched the request path, that no fallback page could + be delivered and that the request ``Content-Type`` was json. + + :param exc: the exception that occurred. + :returns: a WSGI application + """ + error = { + "code": 200, # this code is the JSON-RPC level code, it is + # distinct from the HTTP status code. This + # code is ignored and the value 200 (while + # misleading) is totally arbitrary. + "message": "Odoo Server Error", + "data": serialize_exception(exc), + } + if isinstance(exc, NotFound): + error["code"] = 404 + error["message"] = "404: Not Found" + elif isinstance(exc, SessionExpiredException): + error["code"] = 100 + error["message"] = "Odoo Session Expired" + + return self._response(error=error)