Skip to content

Commit 4f1c5d6

Browse files
improve error handling
1 parent 7e9de14 commit 4f1c5d6

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

services/web/server/src/simcore_service_webserver/licenses/_exceptions_handlers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
exception_handling_decorator,
1010
to_exceptions_handlers_map,
1111
)
12-
from .errors import LicensedItemNotFoundError
12+
from ..wallets.errors import WalletNotEnoughCreditsError
13+
from .errors import LicensedItemNotFoundError, LicensedItemPricingPlanMatchError
1314

1415
_logger = logging.getLogger(__name__)
1516

@@ -23,6 +24,14 @@
2324
status.HTTP_403_FORBIDDEN,
2425
"Wallet {wallet_id} forbidden.",
2526
),
27+
WalletNotEnoughCreditsError: HttpErrorInfo(
28+
status.HTTP_402_PAYMENT_REQUIRED,
29+
"Not enough credits in the wallet.",
30+
),
31+
LicensedItemPricingPlanMatchError: HttpErrorInfo(
32+
status.HTTP_400_BAD_REQUEST,
33+
"The provided pricing plan does not match the one associated with the licensed item.",
34+
),
2635
}
2736

2837

services/web/server/src/simcore_service_webserver/licenses/_licensed_items_api.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
from ..resource_usage.api import get_pricing_plan_unit
2525
from ..users.api import get_user
2626
from ..wallets.api import get_wallet_with_available_credits_by_user_and_wallet
27+
from ..wallets.errors import WalletNotEnoughCreditsError
2728
from . import _licensed_items_api, _licensed_items_db
2829
from ._models import LicensedItemsBodyParams
30+
from .errors import LicensedItemPricingPlanMatchError
2931

3032
_logger = logging.getLogger(__name__)
3133

@@ -95,7 +97,10 @@ async def purchase_licensed_item(
9597
)
9698

9799
if licensed_item.pricing_plan_id != body_params.pricing_plan_id:
98-
raise ValueError("You are lying!")
100+
raise LicensedItemPricingPlanMatchError(
101+
pricing_plan_id=body_params.pricing_plan_id,
102+
licensed_item_id=licensed_item_id,
103+
)
99104

100105
pricing_unit = await get_pricing_plan_unit(
101106
app,
@@ -106,7 +111,9 @@ async def purchase_licensed_item(
106111

107112
# Check whether wallet has enough credits
108113
if wallet.available_credits - pricing_unit.current_cost_per_unit < 0:
109-
raise ValueError("Not enough credits!")
114+
raise WalletNotEnoughCreditsError(
115+
reason=f"Wallet '{wallet.name}' has {wallet.available_credits} credits."
116+
)
110117

111118
user = await get_user(app, user_id=user_id)
112119

services/web/server/src/simcore_service_webserver/licenses/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ class LicensesValueError(WebServerBaseError, ValueError):
77

88
class LicensedItemNotFoundError(LicensesValueError):
99
msg_template = "License good {licensed_item_id} not found"
10+
11+
12+
class LicensedItemPricingPlanMatchError(LicensesValueError):
13+
msg_template = "The provided pricing plan {pricing_plan_id} does not match the one associated with the licensed item {licensed_item_id}."

services/web/server/src/simcore_service_webserver/wallets/_handlers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
MSG_BILLING_DETAILS_NOT_DEFINED_ERROR,
4848
MSG_PRICE_NOT_DEFINED_ERROR,
4949
)
50-
from .errors import WalletAccessForbiddenError, WalletNotFoundError
50+
from .errors import (
51+
WalletAccessForbiddenError,
52+
WalletNotEnoughCreditsError,
53+
WalletNotFoundError,
54+
)
5155

5256
_logger = logging.getLogger(__name__)
5357

@@ -87,6 +91,9 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
8791
except ProductPriceNotDefinedError as exc:
8892
raise web.HTTPConflict(reason=MSG_PRICE_NOT_DEFINED_ERROR) from exc
8993

94+
except WalletNotEnoughCreditsError as exc:
95+
raise web.HTTPPaymentRequired(reason=f"{exc}") from exc
96+
9097
except BillingDetailsNotFoundError as exc:
9198

9299
error_code = create_error_code(exc)

0 commit comments

Comments
 (0)