-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by lmignon
- Loading branch information
Showing
11 changed files
with
341 additions
and
366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from . import models | ||
from . import fastapi_dispatcher | ||
from . import error_handlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,36 @@ | ||
# Copyright 2022 ACSONE SA/NV | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
import logging | ||
|
||
from starlette.exceptions import WebSocketException | ||
from starlette.middleware.errors import ServerErrorMiddleware | ||
from starlette.middleware.exceptions import ExceptionMiddleware | ||
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 starlette.websockets import WebSocket | ||
|
||
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") | ||
) | ||
# we need to monkey patch the ServerErrorMiddleware and ExceptionMiddleware classes | ||
# to ensure that all the exceptions that are handled by these specific | ||
# middlewares are let to bubble up to the retrying mechanism and the | ||
# dispatcher error handler to ensure that appropriate action are taken | ||
# regarding the transaction, environment, and registry. These middlewares | ||
# are added by default by FastAPI when creating an application and it's not | ||
# possible to remove them. So we need to monkey patch them. | ||
|
||
|
||
async def _odoo_missing_error_handler( | ||
request: Request, _exc: odoo.exceptions.MissingError | ||
def pass_through_exception_handler( | ||
self, request: Request, exc: Exception | ||
) -> JSONResponse: | ||
_rollback(request, "MissingError") | ||
return await http_exception_handler( | ||
request, HTTPException(HTTP_404_NOT_FOUND, "MissingError") | ||
) | ||
raise exc | ||
|
||
|
||
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) | ||
def pass_through_websocket_exception_handler( | ||
self, websocket: WebSocket, exc: WebSocketException | ||
) -> None: | ||
raise 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)) | ||
) | ||
ServerErrorMiddleware.error_response = pass_through_exception_handler | ||
ExceptionMiddleware.http_exception = pass_through_exception_handler | ||
ExceptionMiddleware.websocket_exception = pass_through_websocket_exception_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.