import logging from fansifter_common.api.error_handlers import register_defaults from fastapi import FastAPI from starlette.requests import Request from starlette.responses import JSONResponse, Response from preference_center.profile.exceptions import ProfileNotFoundError logger = logging.getLogger(__name__) async def profile_not_found_error_handler( request: Request, # noqa: ARG001 exc: ProfileNotFoundError, ) -> Response: logger.warning( "Profile not found", extra={ "error_code": exc.code, **exc.context, }, ) return JSONResponse( content={ "code": exc.code, "message": exc.message, }, status_code=exc.status_code, ) def register(app: FastAPI) -> None: app.add_exception_handler(ProfileNotFoundError, profile_not_found_error_handler) # type: ignore register_defaults(app)