Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] spp_api: Override the handle_error function #672

Open
wants to merge 1 commit into
base: 17.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spp_api/controllers/apijsonrequest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Copyright 2021 Denis Mudarisov <https://github.com/trojikman>
# 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
Expand All @@ -15,6 +20,23 @@
_logger = logging.getLogger(__name__)


def serialize_exception(exception):
name = type(exception).__name__
module = type(exception).__module__

Check warning on line 25 in spp_api/controllers/apijsonrequest.py

View check run for this annotation

Codecov / codecov/patch

spp_api/controllers/apijsonrequest.py#L24-L25

Added lines #L24 - L25 were not covered by tests

return {

Check warning on line 27 in spp_api/controllers/apijsonrequest.py

View check run for this annotation

Codecov / codecov/patch

spp_api/controllers/apijsonrequest.py#L27

Added line #L27 was not covered by tests
"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"

Expand All @@ -34,3 +56,30 @@
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 = {

Check warning on line 70 in spp_api/controllers/apijsonrequest.py

View check run for this annotation

Codecov / codecov/patch

spp_api/controllers/apijsonrequest.py#L70

Added line #L70 was not covered by tests
"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"

Check warning on line 80 in spp_api/controllers/apijsonrequest.py

View check run for this annotation

Codecov / codecov/patch

spp_api/controllers/apijsonrequest.py#L79-L80

Added lines #L79 - L80 were not covered by tests
elif isinstance(exc, SessionExpiredException):
error["code"] = 100
error["message"] = "Odoo Session Expired"

Check warning on line 83 in spp_api/controllers/apijsonrequest.py

View check run for this annotation

Codecov / codecov/patch

spp_api/controllers/apijsonrequest.py#L82-L83

Added lines #L82 - L83 were not covered by tests

return self._response(error=error)

Check warning on line 85 in spp_api/controllers/apijsonrequest.py

View check run for this annotation

Codecov / codecov/patch

spp_api/controllers/apijsonrequest.py#L85

Added line #L85 was not covered by tests
Loading