Skip to content

Commit 472cc7d

Browse files
committed
error imports
1 parent 6201465 commit 472cc7d

File tree

14 files changed

+135
-67
lines changed

14 files changed

+135
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from pydantic.errors import PydanticErrorMixin
2+
3+
4+
class BaseServiceError(PydanticErrorMixin, ValueError):
5+
@classmethod
6+
def get_full_class_name(cls) -> str:
7+
# Can be used as unique code identifier
8+
return f"{cls.__module__}.{cls.__name__}"
9+
10+
11+
#
12+
# service wide errors
13+
#
14+
15+
16+
class PaymentServiceUnavailableError(BaseServiceError):
17+
msg_template = "Payments are currently unavailable: {human_reason}"
18+
19+
20+
#
21+
# payments
22+
#
23+
24+
25+
class PaymentsError(BaseServiceError):
26+
msg_template = "Error in payment transaction '{payment_id}'"
27+
28+
29+
class PaymentNotFoundError(PaymentsError):
30+
msg_template = "Payment transaction '{payment_id}' was not found"
31+
32+
33+
class PaymentAlreadyExistsError(PaymentsError):
34+
msg_template = "Payment transaction '{payment_id}' was already initialized"
35+
36+
37+
class PaymentAlreadyAckedError(PaymentsError):
38+
msg_template = "Payment transaction '{payment_id}' cannot be changes since it was already closed."
39+
40+
41+
#
42+
# payment-methods
43+
#
44+
45+
46+
class PaymentsMethodsError(BaseServiceError):
47+
...
48+
49+
50+
class PaymentMethodNotFoundError(PaymentsMethodsError):
51+
msg_template = "The specified payment method '{payment_method_id}' does not exist"
52+
53+
54+
class PaymentMethodAlreadyAckedError(PaymentsMethodsError):
55+
msg_template = (
56+
"Cannot create payment-method '{payment_method_id}' since it was already closed"
57+
)
58+
59+
60+
class PaymentMethodUniqueViolationError(PaymentsMethodsError):
61+
msg_template = "Payment method '{payment_method_id}' aready exists"
62+
63+
64+
class InvalidPaymentMethodError(PaymentsMethodsError):
65+
msg_template = "Invalid payment method '{payment_method_id}'"

services/payments/src/simcore_service_payments/api/rest/_acknowledgements.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
from typing import Annotated
33

44
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
5+
from models_library.api_schemas_payments.errors import (
6+
PaymentMethodNotFoundError,
7+
PaymentNotFoundError,
8+
)
59
from servicelib.logging_utils import log_context
610

711
from ..._constants import ACKED, PGDB
8-
from ...core.errors import PaymentMethodNotFoundError, PaymentNotFoundError
912
from ...db.payments_methods_repo import PaymentsMethodsRepo
1013
from ...db.payments_transactions_repo import PaymentsTransactionsRepo
1114
from ...models.auth import SessionData

services/payments/src/simcore_service_payments/api/rest/_dependencies.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from fastapi import Depends, FastAPI, Request
66
from fastapi.security import OAuth2PasswordBearer
7+
from servicelib.fastapi.app_state import AppStateMixin
78
from servicelib.fastapi.dependencies import get_app, get_reverse_url_mapper
8-
from servicelib.fastapi.http_client import AppStateMixin
99
from sqlalchemy.ext.asyncio import AsyncEngine
1010

1111
from ..._meta import API_VTAG

services/payments/src/simcore_service_payments/api/rpc/_payments.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from decimal import Decimal
33

44
from fastapi import FastAPI
5+
from models_library.api_schemas_payments.errors import (
6+
PaymentsError,
7+
PaymentServiceUnavailableError,
8+
)
59
from models_library.api_schemas_webserver.wallets import (
610
PaymentID,
711
PaymentTransaction,
@@ -23,7 +27,7 @@
2327
router = RPCRouter()
2428

2529

26-
@router.expose()
30+
@router.expose(reraise_if_error_type=(PaymentsError, PaymentServiceUnavailableError))
2731
async def init_payment(
2832
app: FastAPI,
2933
*,
@@ -60,7 +64,7 @@ async def init_payment(
6064
)
6165

6266

63-
@router.expose()
67+
@router.expose(reraise_if_error_type=(PaymentsError, PaymentServiceUnavailableError))
6468
async def cancel_payment(
6569
app: FastAPI,
6670
*,
@@ -85,7 +89,7 @@ async def cancel_payment(
8589
)
8690

8791

88-
@router.expose()
92+
@router.expose(reraise_if_error_type=(PaymentsError, PaymentServiceUnavailableError))
8993
async def get_payments_page(
9094
app: FastAPI,
9195
*,

services/payments/src/simcore_service_payments/api/rpc/_payments_methods.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
from decimal import Decimal
33

44
from fastapi import FastAPI
5+
from models_library.api_schemas_payments.errors import (
6+
PaymentsError,
7+
PaymentServiceUnavailableError,
8+
PaymentsMethodsError,
9+
)
510
from models_library.api_schemas_webserver.wallets import (
611
PaymentMethodGet,
712
PaymentMethodID,
@@ -26,7 +31,9 @@
2631
router = RPCRouter()
2732

2833

29-
@router.expose()
34+
@router.expose(
35+
reraise_if_error_type=(PaymentsMethodsError, PaymentServiceUnavailableError)
36+
)
3037
async def init_creation_of_payment_method(
3138
app: FastAPI,
3239
*,
@@ -54,7 +61,9 @@ async def init_creation_of_payment_method(
5461
)
5562

5663

57-
@router.expose()
64+
@router.expose(
65+
reraise_if_error_type=(PaymentsMethodsError, PaymentServiceUnavailableError)
66+
)
5867
async def cancel_creation_of_payment_method(
5968
app: FastAPI,
6069
*,
@@ -78,7 +87,9 @@ async def cancel_creation_of_payment_method(
7887
)
7988

8089

81-
@router.expose()
90+
@router.expose(
91+
reraise_if_error_type=(PaymentsMethodsError, PaymentServiceUnavailableError)
92+
)
8293
async def list_payment_methods(
8394
app: FastAPI,
8495
*,
@@ -93,7 +104,9 @@ async def list_payment_methods(
93104
)
94105

95106

96-
@router.expose()
107+
@router.expose(
108+
reraise_if_error_type=(PaymentsMethodsError, PaymentServiceUnavailableError)
109+
)
97110
async def get_payment_method(
98111
app: FastAPI,
99112
*,
@@ -110,7 +123,9 @@ async def get_payment_method(
110123
)
111124

112125

113-
@router.expose()
126+
@router.expose(
127+
reraise_if_error_type=(PaymentsMethodsError, PaymentServiceUnavailableError)
128+
)
114129
async def delete_payment_method(
115130
app: FastAPI,
116131
*,
@@ -127,7 +142,13 @@ async def delete_payment_method(
127142
)
128143

129144

130-
@router.expose()
145+
@router.expose(
146+
reraise_if_error_type=(
147+
PaymentsMethodsError,
148+
PaymentsError,
149+
PaymentServiceUnavailableError,
150+
)
151+
)
131152
async def pay_with_payment_method( # noqa: PLR0913 # pylint: disable=too-many-arguments
132153
app: FastAPI,
133154
*,
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
1-
from pydantic.errors import PydanticErrorMixin
1+
from models_library.api_schemas_payments.errors import BaseServiceError
22

3+
#
4+
# gateway errors
5+
#
36

4-
class PaymentsError(PydanticErrorMixin, ValueError):
5-
msg_template = "Error in payment transaction '{payment_id}'"
67

7-
8-
class PaymentNotFoundError(PaymentsError):
9-
msg_template = "Payment transaction '{payment_id}' was not found"
10-
11-
12-
class PaymentAlreadyExistsError(PaymentsError):
13-
msg_template = "Payment transaction '{payment_id}' was already initialized"
14-
15-
16-
class PaymentAlreadyAckedError(PaymentsError):
17-
msg_template = "Payment transaction '{payment_id}' cannot be changes since it was already closed."
18-
19-
20-
class PaymentsMethodsError(PaymentsError):
8+
class PaymentsGatewayError(BaseServiceError):
219
...
2210

2311

24-
class PaymentMethodNotFoundError(PaymentsMethodsError):
25-
msg_template = "The specified payment method '{payment_method_id}' does not exist"
26-
27-
28-
class PaymentMethodAlreadyAckedError(PaymentsMethodsError):
29-
msg_template = (
30-
"Cannot create payment-method '{payment_method_id}' since it was already closed"
31-
)
32-
33-
34-
class PaymentMethodUniqueViolationError(PaymentsMethodsError):
35-
msg_template = "Payment method '{payment_method_id}' aready exists"
36-
37-
38-
class InvalidPaymentMethodError(PaymentsMethodsError):
39-
msg_template = "Invalid payment method '{payment_method_id}'"
12+
class PaymentsGatewayNotReadyError(PaymentsGatewayError):
13+
msg_template = "Payments-Gateway is unresponsive: {checks}"

services/payments/src/simcore_service_payments/db/auto_recharge_repo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from typing import TypeAlias
22

3+
from models_library.api_schemas_payments.errors import InvalidPaymentMethodError
34
from models_library.api_schemas_webserver.wallets import PaymentMethodID
45
from models_library.basic_types import NonNegativeDecimal
56
from models_library.users import UserID
67
from models_library.wallets import WalletID
78
from pydantic import BaseModel, PositiveInt
89
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStmts
910

10-
from ..core.errors import InvalidPaymentMethodError
1111
from .base import BaseRepository
1212

1313
AutoRechargeID: TypeAlias = PositiveInt

services/payments/src/simcore_service_payments/db/payments_methods_repo.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import simcore_postgres_database.errors as db_errors
44
import sqlalchemy as sa
55
from arrow import utcnow
6+
from models_library.api_schemas_payments.errors import (
7+
PaymentMethodAlreadyAckedError,
8+
PaymentMethodNotFoundError,
9+
PaymentMethodUniqueViolationError,
10+
)
611
from models_library.api_schemas_webserver.wallets import PaymentMethodID
712
from models_library.users import UserID
813
from models_library.wallets import WalletID
@@ -12,11 +17,6 @@
1217
payments_methods,
1318
)
1419

15-
from ..core.errors import (
16-
PaymentMethodAlreadyAckedError,
17-
PaymentMethodNotFoundError,
18-
PaymentMethodUniqueViolationError,
19-
)
2020
from ..models.db import PaymentsMethodsDB
2121
from .base import BaseRepository
2222

services/payments/src/simcore_service_payments/db/payments_transactions_repo.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
from decimal import Decimal
33

44
import sqlalchemy as sa
5+
from models_library.api_schemas_payments.errors import (
6+
PaymentAlreadyAckedError,
7+
PaymentAlreadyExistsError,
8+
PaymentNotFoundError,
9+
)
510
from models_library.api_schemas_webserver.wallets import PaymentID
611
from models_library.users import UserID
712
from models_library.wallets import WalletID
@@ -12,11 +17,6 @@
1217
payments_transactions,
1318
)
1419

15-
from ..core.errors import (
16-
PaymentAlreadyAckedError,
17-
PaymentAlreadyExistsError,
18-
PaymentNotFoundError,
19-
)
2020
from ..models.db import PaymentsTransactionsDB
2121
from .base import BaseRepository
2222

services/payments/src/simcore_service_payments/services/notifier.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
PaymentTransaction,
1414
)
1515
from models_library.users import UserID
16-
from servicelib.fastapi.http_client import AppStateMixin
16+
from servicelib.fastapi.app_state import AppStateMixin
1717

1818
from ..db.payment_users_repo import PaymentsUsersRepo
1919
from .postgres import get_engine

services/payments/src/simcore_service_payments/services/payments.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
from decimal import Decimal
1111

1212
import arrow
13+
from models_library.api_schemas_payments.errors import (
14+
PaymentAlreadyAckedError,
15+
PaymentAlreadyExistsError,
16+
PaymentNotFoundError,
17+
)
1318
from models_library.api_schemas_webserver.wallets import (
1419
PaymentID,
1520
PaymentMethodID,
@@ -29,11 +34,6 @@
2934
from tenacity.stop import stop_after_attempt
3035

3136
from .._constants import RUT
32-
from ..core.errors import (
33-
PaymentAlreadyAckedError,
34-
PaymentAlreadyExistsError,
35-
PaymentNotFoundError,
36-
)
3737
from ..db.payments_transactions_repo import PaymentsTransactionsRepo
3838
from ..models.db import PaymentsTransactionsDB
3939
from ..models.db_to_api import to_payments_api_model

services/payments/src/simcore_service_payments/services/resource_usage_tracker.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from models_library.resource_tracker import CreditTransactionId
2020
from models_library.users import UserID
2121
from models_library.wallets import WalletID
22-
from servicelib.fastapi.http_client import AppStateMixin, BaseHttpApi
22+
from servicelib.fastapi.app_state import AppStateMixin
23+
from servicelib.fastapi.http_client import BaseHttpApi
2324

2425
from ..core.settings import ApplicationSettings
2526

services/payments/tests/unit/api/test_rest_acknowledgements.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import pytest
1212
from faker import Faker
1313
from fastapi import FastAPI, status
14-
from pytest_mock import MockerFixture
15-
from pytest_simcore.helpers.typing_env import EnvVarsDict
16-
from pytest_simcore.helpers.utils_envs import setenvs_from_dict
17-
from simcore_service_payments.core.errors import (
14+
from models_library.api_schemas_payments.errors import (
1815
PaymentMethodNotFoundError,
1916
PaymentNotFoundError,
2017
)
18+
from pytest_mock import MockerFixture
19+
from pytest_simcore.helpers.typing_env import EnvVarsDict
20+
from pytest_simcore.helpers.utils_envs import setenvs_from_dict
2121
from simcore_service_payments.models.schemas.acknowledgements import (
2222
AckPayment,
2323
AckPaymentMethod,

services/payments/tests/unit/test_rpc_payments.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111
from faker import Faker
1212
from fastapi import FastAPI
13+
from models_library.api_schemas_payments.errors import PaymentNotFoundError
1314
from models_library.api_schemas_webserver.wallets import WalletPaymentInitiated
1415
from models_library.rabbitmq_basic_types import RPCMethodName
1516
from pydantic import parse_obj_as
@@ -18,7 +19,6 @@
1819
from respx import MockRouter
1920
from servicelib.rabbitmq import RabbitMQRPCClient, RPCServerError
2021
from simcore_service_payments.api.rpc.routes import PAYMENTS_RPC_NAMESPACE
21-
from simcore_service_payments.core.errors import PaymentNotFoundError
2222

2323
pytest_simcore_core_services_selection = [
2424
"postgres",

0 commit comments

Comments
 (0)