Skip to content

Commit 41ebbf4

Browse files
IlyasDevelopmentIlyas Gasanov
andauthored
[DOP-19778] Common error format (#95)
Co-authored-by: Ilyas Gasanov <izgasanov@mts.ru>
1 parent 09e1172 commit 41ebbf4

52 files changed

Lines changed: 1891 additions & 986 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement a single error handling format to improve consistency

syncmaster/backend/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
22
# SPDX-License-Identifier: Apache-2.0
33
from fastapi import FastAPI, HTTPException
4+
from fastapi.exceptions import RequestValidationError
5+
from pydantic import ValidationError
46
from starlette.middleware.cors import CORSMiddleware
57

68
from syncmaster.backend.api.deps import (
@@ -13,6 +15,8 @@
1315
from syncmaster.backend.handler import (
1416
http_exception_handler,
1517
syncmsater_exception_handler,
18+
unknown_exception_handler,
19+
validation_exception_handler,
1620
)
1721
from syncmaster.config import Settings
1822
from syncmaster.db.factory import create_engine, create_session_factory, get_uow
@@ -33,8 +37,11 @@ def application_factory(settings: Settings) -> FastAPI:
3337
)
3438

3539
application.include_router(api_router)
36-
application.exception_handler(HTTPException)(http_exception_handler)
40+
application.exception_handler(RequestValidationError)(validation_exception_handler)
41+
application.exception_handler(ValidationError)(validation_exception_handler)
3742
application.exception_handler(SyncmasterError)(syncmsater_exception_handler)
43+
application.exception_handler(HTTPException)(http_exception_handler)
44+
application.exception_handler(Exception)(unknown_exception_handler)
3845

3946
engine = create_engine(connection_uri=settings.build_db_connection_uri())
4047
session_factory = create_session_factory(engine=engine)

syncmaster/backend/api/monitoring.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from fastapi import APIRouter
44

5-
router = APIRouter(tags=["monitoring"], prefix="/monitoring")
5+
from syncmaster.errors.registration import get_error_responses
6+
7+
router = APIRouter(tags=["monitoring"], prefix="/monitoring", responses=get_error_responses())
68

79

810
@router.get("/ping")

syncmaster/backend/api/v1/auth/router.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
from syncmaster.backend.api.v1.auth.utils import sign_jwt
88
from syncmaster.backend.services import UnitOfWork
99
from syncmaster.config import Settings
10+
from syncmaster.errors.registration import get_error_responses
11+
from syncmaster.errors.schemas.invalid_request import InvalidRequestSchema
12+
from syncmaster.errors.schemas.not_authorized import NotAuthorizedSchema
1013
from syncmaster.exceptions import EntityNotFoundError
1114
from syncmaster.schemas.v1.auth import AuthTokenSchema
1215

13-
router = APIRouter(prefix="/auth", tags=["Auth"])
16+
router = APIRouter(
17+
prefix="/auth",
18+
tags=["Auth"],
19+
responses=get_error_responses(include={NotAuthorizedSchema, InvalidRequestSchema}),
20+
)
1421

1522

1623
@router.post("/token")

syncmaster/backend/api/v1/connections.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from syncmaster.backend.services import UnitOfWork, get_user
1010
from syncmaster.db.models import Connection, Transfer, User
1111
from syncmaster.db.utils import Permission
12+
from syncmaster.errors.registration import get_error_responses
1213
from syncmaster.exceptions import ActionNotAllowedError
1314
from syncmaster.exceptions.connection import (
1415
ConnectionDeleteError,
@@ -34,7 +35,7 @@
3435
from syncmaster.schemas.v1.page import MetaPageSchema
3536
from syncmaster.schemas.v1.status import StatusResponseSchema
3637

37-
router = APIRouter(tags=["Connections"])
38+
router = APIRouter(tags=["Connections"], responses=get_error_responses())
3839

3940
CONNECTION_TYPES = ORACLE_TYPE, POSTGRES_TYPE, HIVE_TYPE, S3_TYPE, HDFS_TYPE
4041

syncmaster/backend/api/v1/groups.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from syncmaster.backend.services import UnitOfWork, get_user
77
from syncmaster.db.models import User
88
from syncmaster.db.utils import Permission
9+
from syncmaster.errors.registration import get_error_responses
910
from syncmaster.exceptions import ActionNotAllowedError
1011
from syncmaster.exceptions.group import GroupNotFoundError
1112
from syncmaster.schemas.v1.groups import (
@@ -18,7 +19,7 @@
1819
from syncmaster.schemas.v1.status import StatusResponseSchema
1920
from syncmaster.schemas.v1.users import UserPageSchemaAsGroupMember
2021

21-
router = APIRouter(tags=["Groups"])
22+
router = APIRouter(tags=["Groups"], responses=get_error_responses())
2223

2324

2425
@router.get("/groups")

syncmaster/backend/api/v1/queue.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from syncmaster.backend.services import UnitOfWork, get_user
77
from syncmaster.db.models import User
88
from syncmaster.db.utils import Permission
9+
from syncmaster.errors.registration import get_error_responses
910
from syncmaster.exceptions import ActionNotAllowedError
1011
from syncmaster.exceptions.group import GroupNotFoundError
1112
from syncmaster.exceptions.queue import QueueDeleteError, QueueNotFoundError
@@ -17,7 +18,7 @@
1718
)
1819
from syncmaster.schemas.v1.status import StatusResponseSchema
1920

20-
router = APIRouter(tags=["Queues"])
21+
router = APIRouter(tags=["Queues"], responses=get_error_responses())
2122

2223

2324
@router.get("/queues/{queue_id}", description="Read queue by id")

syncmaster/backend/api/v1/transfers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from syncmaster.backend.services import UnitOfWork, get_user
99
from syncmaster.db.models import Status, User
1010
from syncmaster.db.utils import Permission
11+
from syncmaster.errors.registration import get_error_responses
1112
from syncmaster.exceptions.base import ActionNotAllowedError
1213
from syncmaster.exceptions.connection import ConnectionNotFoundError
1314
from syncmaster.exceptions.group import GroupNotFoundError
@@ -38,7 +39,7 @@
3839
)
3940
from syncmaster.worker.config import celery
4041

41-
router = APIRouter(tags=["Transfers"])
42+
router = APIRouter(tags=["Transfers"], responses=get_error_responses())
4243

4344

4445
@router.get("/transfers")

syncmaster/backend/api/v1/users.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
from syncmaster.backend.api.deps import UnitOfWorkMarker
88
from syncmaster.backend.services import UnitOfWork, get_user
99
from syncmaster.db.models import User
10+
from syncmaster.errors.registration import get_error_responses
1011
from syncmaster.schemas.v1.users import ReadUserSchema, UserPageSchema
1112

1213
logger = logging.getLogger(__name__)
1314

1415

15-
router = APIRouter(tags=["Users"])
16+
router = APIRouter(tags=["Users"], responses=get_error_responses())
1617

1718

1819
@router.get("/users")

0 commit comments

Comments
 (0)