forked from OpenG2P/rest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_handlers.py
80 lines (60 loc) · 2.22 KB
/
error_handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2022 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from starlette.responses import JSONResponse
from starlette.status import (
HTTP_400_BAD_REQUEST,
HTTP_403_FORBIDDEN,
HTTP_404_NOT_FOUND,
HTTP_500_INTERNAL_SERVER_ERROR,
)
import odoo
from fastapi import Request
from fastapi.exception_handlers import http_exception_handler
from fastapi.exceptions import HTTPException
from .context import odoo_env_ctx
_logger = logging.getLogger(__name__)
def _rollback(request: Request, reason: str) -> None:
cr = odoo_env_ctx.get().cr
if cr is not None:
_logger.debug("rollback on %s", reason)
cr.rollback()
async def _odoo_user_error_handler(
request: Request, exc: odoo.exceptions.UserError
) -> JSONResponse:
_rollback(request, "UserError")
return await http_exception_handler(
request, HTTPException(HTTP_400_BAD_REQUEST, exc.args[0])
)
async def _odoo_access_error_handler(
request: Request, _exc: odoo.exceptions.AccessError
) -> JSONResponse:
_rollback(request, "AccessError")
return await http_exception_handler(
request, HTTPException(HTTP_403_FORBIDDEN, "AccessError")
)
async def _odoo_missing_error_handler(
request: Request, _exc: odoo.exceptions.MissingError
) -> JSONResponse:
_rollback(request, "MissingError")
return await http_exception_handler(
request, HTTPException(HTTP_404_NOT_FOUND, "MissingError")
)
async def _odoo_validation_error_handler(
request: Request, exc: odoo.exceptions.ValidationError
) -> JSONResponse:
_rollback(request, "ValidationError")
return await http_exception_handler(
request, HTTPException(HTTP_400_BAD_REQUEST, exc.args[0])
)
async def _odoo_http_exception_handler(
request: Request, exc: HTTPException
) -> JSONResponse:
_rollback(request, "HTTPException")
return await http_exception_handler(request, exc)
async def _odoo_exception_handler(request: Request, exc: Exception) -> JSONResponse:
_rollback(request, "Exception")
_logger.exception("Unhandled exception", exc_info=exc)
return await http_exception_handler(
request, HTTPException(HTTP_500_INTERNAL_SERVER_ERROR, str(exc))
)