Skip to content

Commit

Permalink
refacotr errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 committed Nov 1, 2024
1 parent 18c5fb9 commit 5f14650
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 71 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from ..api.rest.routes import setup_api_routes
from ..api.rpc.routes import setup_rpc_api_routes
from ..exceptions.handlers import setup_exception_handlers
from ..services.background_tasks_setup import setup as setup_background_tasks
from ..services.modules.db import setup as setup_db
from ..services.modules.rabbitmq import setup as setup_rabbitmq
Expand Down Expand Up @@ -68,7 +69,7 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
)

# ERROR HANDLERS
# app.add_exception_handler(CustomResourceUsageTrackerError, http404_error_handler)
setup_exception_handlers(app)

# EVENTS
async def _on_startup() -> None:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class ResourceUsageTrackerBaseError(OsparcErrorMixin, Exception):
msg_template = "Resource Usage Tracker unknown error"


class ConfigurationError(ResourceUsageTrackerBaseError):
msg_template = "Resource usage Tracker configuration error"


### NotCreatedDBError


Expand Down Expand Up @@ -56,7 +60,11 @@ class PricingUnitCostDoesNotExistsDBError(ResourceUsageTrackerBaseError):
### NotFoundError


class PricingPlanNotFoundForServiceError(ResourceUsageTrackerBaseError):
class RutNotFoundError(ResourceUsageTrackerBaseError):
...


class PricingPlanNotFoundForServiceError(RutNotFoundError):
msg_template = (
"Pricing plan not found for service key {service_key} version {service_version}"
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from fastapi import FastAPI, HTTPException, status
from fastapi.exceptions import RequestValidationError

from ._http_error import http_error_handler, make_http_error_handler_for_exception
from ._validation_error import http422_error_handler
from ._http_error import (
RutNotFoundError,
http404_error_handler,
http_error_handler,
make_http_error_handler_for_exception,
)


def setup_exception_handlers(app: FastAPI) -> None:
app.add_exception_handler(HTTPException, http_error_handler)
app.add_exception_handler(RequestValidationError, http422_error_handler)
app.add_exception_handler(RutNotFoundError, http404_error_handler)

app.add_exception_handler(
Exception,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from collections.abc import Callable
from typing import Awaitable

from fastapi import HTTPException
from fastapi import HTTPException, Request, status
from fastapi.encoders import jsonable_encoder
from starlette.requests import Request
from starlette.responses import JSONResponse

from ...exceptions.errors import RutNotFoundError


async def http_error_handler(_: Request, exc: Exception) -> JSONResponse:
assert isinstance(exc, HTTPException) # nosec
Expand All @@ -14,6 +15,16 @@ async def http_error_handler(_: Request, exc: Exception) -> JSONResponse:
)


def http404_error_handler(
_: Request, # pylint: disable=unused-argument
exc: RutNotFoundError,
) -> JSONResponse:
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content={"message": f"{exc.msg_template}"},
)


def make_http_error_handler_for_exception(
status_code: int, exception_cls: type[BaseException]
) -> Callable[[Request, type[BaseException]], Awaitable[JSONResponse]]:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from settings_library.rabbit import RabbitSettings

from ...core.errors import ConfigurationError
from ...exceptions.errors import ConfigurationError

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
wait_random_exponential,
)

from ...core.errors import ConfigurationError
from ...exceptions.errors import ConfigurationError

_logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ async def test_rpc_list_service_runs_raising_custom_error(
resource_tracker_setup_db: dict,
rpc_client: RabbitMQRPCClient,
):
with pytest.raises(RPCServerError):
with pytest.raises(RPCServerError) as e:
await service_runs.get_service_run_page(
rpc_client,
user_id=_USER_ID,
product_name="osparc",
access_all_wallet_usage=True,
)
assert e
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
parse_request_path_parameters_as,
)
from servicelib.aiohttp.typing_extension import Handler
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
CustomResourceUsageTrackerError,
)
from servicelib.rabbitmq._errors import RPCServerError
from servicelib.request_keys import RQT_USERID_KEY

from .._constants import RQ_PRODUCT_KEY
Expand All @@ -49,8 +47,8 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
try:
return await handler(request)

except CustomResourceUsageTrackerError as exc:
raise CustomResourceUsageTrackerError from exc
except RPCServerError as exc:
raise RPCServerError from exc

return wrapper

Expand Down

0 comments on commit 5f14650

Please sign in to comment.