Skip to content

Commit 5847d66

Browse files
committed
exception handlers setup
1 parent 7b23e09 commit 5847d66

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

services/api-server/src/simcore_service_api_server/core/application.py

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import logging
22

33
from fastapi import FastAPI
4-
from fastapi.exceptions import RequestValidationError
54
from fastapi_pagination import add_pagination
6-
from httpx import HTTPError as HttpxException
7-
from models_library.basic_types import BootModeEnum
85
from servicelib.fastapi.profiler_middleware import ProfilerMiddleware
96
from servicelib.logging_utils import config_all_loggers
10-
from starlette import status
11-
from starlette.exceptions import HTTPException
127

138
from .._meta import API_VERSION, API_VTAG
14-
from ..api.errors.custom_errors import custom_error_handler
15-
from ..api.errors.http_error import (
16-
http_error_handler,
17-
make_http_error_handler_for_exception,
18-
)
19-
from ..api.errors.httpx_client_error import handle_httpx_client_exceptions
20-
from ..api.errors.log_handling_error import log_handling_error_handler
21-
from ..api.errors.validation_error import http422_error_handler
229
from ..api.root import create_router
2310
from ..api.routes.health import router as health_router
24-
from ..models.custom_errors import CustomBaseError
11+
from ..errors import exception_handlers
2512
from ..services import catalog, director_v2, storage, webserver
26-
from ..services.log_streaming import LogDistributionBaseException
2713
from ..services.rabbitmq import setup_rabbitmq
2814
from ._prometheus_instrumentation import setup_prometheus_instrumentation
2915
from .events import create_start_app_handler, create_stop_app_handler
@@ -97,31 +83,8 @@ def init_app(settings: ApplicationSettings | None = None) -> FastAPI:
9783
app.add_event_handler("startup", create_start_app_handler(app))
9884
app.add_event_handler("shutdown", create_stop_app_handler(app))
9985

100-
app.add_exception_handler(HTTPException, http_error_handler)
101-
app.add_exception_handler(HttpxException, handle_httpx_client_exceptions)
102-
app.add_exception_handler(RequestValidationError, http422_error_handler)
103-
app.add_exception_handler(LogDistributionBaseException, log_handling_error_handler)
104-
app.add_exception_handler(CustomBaseError, custom_error_handler)
105-
106-
# SEE https://docs.python.org/3/library/exceptions.html#exception-hierarchy
107-
app.add_exception_handler(
108-
NotImplementedError,
109-
make_http_error_handler_for_exception(
110-
NotImplementedError,
111-
status.HTTP_501_NOT_IMPLEMENTED,
112-
detail_message="Endpoint not implemented",
113-
),
114-
)
115-
app.add_exception_handler(
116-
Exception,
117-
make_http_error_handler_for_exception(
118-
Exception,
119-
status.HTTP_500_INTERNAL_SERVER_ERROR,
120-
detail_message="Unexpected error",
121-
add_exception_to_message=(settings.SC_BOOT_MODE == BootModeEnum.DEBUG),
122-
add_oec_to_message=True,
123-
),
124-
)
86+
exception_handlers.setup(app)
87+
12588
if settings.API_SERVER_PROFILING:
12689
app.add_middleware(ProfilerMiddleware)
12790

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from fastapi import FastAPI
2+
from fastapi.exceptions import RequestValidationError
3+
from httpx import HTTPError as HttpxException
4+
from models_library.basic_types import BootModeEnum
5+
from starlette import status
6+
from starlette.exceptions import HTTPException
7+
8+
from ..core.settings import ApplicationSettings
9+
from ..models.custom_errors import CustomBaseError
10+
from ..services.log_streaming import LogDistributionBaseException
11+
from .custom_errors import custom_error_handler
12+
from .http_error import http_error_handler, make_http_error_handler_for_exception
13+
from .httpx_client_error import handle_httpx_client_exceptions
14+
from .log_handling_error import log_handling_error_handler
15+
from .validation_error import http422_error_handler
16+
17+
18+
def setup(app: FastAPI):
19+
settings: ApplicationSettings = app.state.settings
20+
assert isinstance(settings, ApplicationSettings) # nosec
21+
22+
app.add_exception_handler(HTTPException, http_error_handler)
23+
app.add_exception_handler(HttpxException, handle_httpx_client_exceptions)
24+
app.add_exception_handler(RequestValidationError, http422_error_handler)
25+
app.add_exception_handler(LogDistributionBaseException, log_handling_error_handler)
26+
app.add_exception_handler(CustomBaseError, custom_error_handler)
27+
28+
# SEE https://docs.python.org/3/library/exceptions.html#exception-hierarchy
29+
app.add_exception_handler(
30+
NotImplementedError,
31+
make_http_error_handler_for_exception(
32+
NotImplementedError,
33+
status.HTTP_501_NOT_IMPLEMENTED,
34+
detail_message="Endpoint not implemented",
35+
),
36+
)
37+
app.add_exception_handler(
38+
Exception,
39+
make_http_error_handler_for_exception(
40+
Exception,
41+
status.HTTP_500_INTERNAL_SERVER_ERROR,
42+
detail_message="Unexpected error",
43+
add_exception_to_message=(settings.SC_BOOT_MODE == BootModeEnum.DEBUG),
44+
add_oec_to_message=True,
45+
),
46+
)

0 commit comments

Comments
 (0)