Skip to content

Commit

Permalink
🎨 improve error handling on pricing plans (#6436)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 authored Sep 25, 2024
1 parent b6532c1 commit ccdeff2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ async def get_service_resources(request: Request):
)
@login_required
@permission_required("services.catalog.*")
@_handlers_errors.reraise_catalog_exceptions_as_http_errors
async def get_service_pricing_plan(request: Request):
ctx = CatalogRequestContext.create(request)
path_params = parse_request_path_parameters_as(ServicePathParams, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from aiohttp import web
from servicelib.aiohttp.typing_extension import Handler

from ..resource_usage.errors import DefaultPricingPlanNotFoundError
from .exceptions import (
CatalogForbiddenError,
CatalogItemNotFoundError,
Expand All @@ -19,6 +20,7 @@ async def _wrapper(request: web.Request) -> web.StreamResponse:

except (
CatalogItemNotFoundError,
DefaultPricingPlanNotFoundError,
DefaultPricingUnitForServiceNotFoundError,
) as exc:
raise web.HTTPNotFound(reason=f"{exc}") from exc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from ..groups.exceptions import GroupNotFoundError
from ..login.decorators import login_required
from ..projects.api import has_user_project_access_rights
from ..resource_usage.errors import DefaultPricingPlanNotFoundError
from ..security.decorators import permission_required
from ..users.api import get_user_id_from_gid, get_user_role
from ..users.exceptions import UserDefaultWalletNotFoundError
Expand Down Expand Up @@ -101,6 +102,7 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
ProjectNotFoundError,
NodeNotFoundError,
UserDefaultWalletNotFoundError,
DefaultPricingPlanNotFoundError,
DefaultPricingUnitNotFoundError,
GroupNotFoundError,
CatalogItemNotFoundError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from ..login.decorators import login_required
from ..notifications import project_logs
from ..products.api import Product, get_current_product
from ..resource_usage.errors import DefaultPricingPlanNotFoundError
from ..security.decorators import permission_required
from ..users import api
from ..users.exceptions import UserDefaultWalletNotFoundError
Expand All @@ -37,6 +38,7 @@
from . import projects_api
from ._common_models import ProjectPathParams, RequestContext
from .exceptions import (
DefaultPricingUnitNotFoundError,
ProjectInvalidRightsError,
ProjectNotFoundError,
ProjectStartsTooManyDynamicNodesError,
Expand All @@ -57,7 +59,12 @@ async def _wrapper(request: web.Request) -> web.StreamResponse:
try:
return await handler(request)

except (ProjectNotFoundError, UserDefaultWalletNotFoundError) as exc:
except (
ProjectNotFoundError,
UserDefaultWalletNotFoundError,
DefaultPricingPlanNotFoundError,
DefaultPricingUnitNotFoundError,
) as exc:
raise web.HTTPNotFound(reason=f"{exc}") from exc

except ProjectInvalidRightsError as exc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
from models_library.users import UserID
from models_library.wallets import WalletID
from pydantic import NonNegativeInt, parse_obj_as
from servicelib.aiohttp import status
from servicelib.aiohttp.client_session import get_client_session
from settings_library.resource_usage_tracker import ResourceUsageTrackerSettings
from yarl import URL

from ._utils import handle_client_exceptions
from .errors import DefaultPricingPlanNotFoundError
from .settings import get_plugin_settings

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -95,10 +97,15 @@ async def get_default_service_pricing_plan(
}
)
with handle_client_exceptions(app) as session:
async with session.get(url) as response:
response.raise_for_status()
body: dict = await response.json()
return parse_obj_as(PricingPlanGet, body)
try:
async with session.get(url) as response:
response.raise_for_status()
body: dict = await response.json()
return parse_obj_as(PricingPlanGet, body)
except ClientResponseError as e:
if e.status == status.HTTP_404_NOT_FOUND:
raise DefaultPricingPlanNotFoundError from e
raise


async def get_pricing_plan_unit(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from ..errors import WebServerBaseError


class ResourceUsageValueError(WebServerBaseError, ValueError):
...


class DefaultPricingPlanNotFoundError(ResourceUsageValueError):
msg_template = "Default pricing plan not found"

0 comments on commit ccdeff2

Please sign in to comment.