diff --git a/backend/auth/api_key_handler.py b/backend/auth/api_key_handler.py index b274aa762..16d109c88 100644 --- a/backend/auth/api_key_handler.py +++ b/backend/auth/api_key_handler.py @@ -20,7 +20,6 @@ async def verify_api_key( result.data[0]["creation_time"], "%Y-%m-%dT%H:%M:%S" ).date() - # Check if the API key was created in the month of the current date if (api_key_creation_date.month == current_date.month) and ( api_key_creation_date.year == current_date.year ): @@ -35,7 +34,6 @@ async def get_user_from_api_key( ) -> UserIdentity: supabase_db = get_supabase_db() - # Lookup the user_id from the api_keys table user_id_data = supabase_db.get_user_id_by_api_key(api_key) if not user_id_data.data: @@ -43,7 +41,6 @@ async def get_user_from_api_key( user_id = user_id_data.data[0]["user_id"] - # Lookup the email from the users table. Todo: remove and use user_id for credentials email = supabase_db.get_user_email(user_id) return UserIdentity(email=email, id=user_id) diff --git a/backend/chat_service.py b/backend/chat_service.py index 53b9a7543..00a37b0d3 100644 --- a/backend/chat_service.py +++ b/backend/chat_service.py @@ -1,5 +1,7 @@ import os +from utils import handle_request_validation_error + if __name__ == "__main__": # import needed here when running main.py to debug backend # you will need to run pip install python-dotenv @@ -7,8 +9,7 @@ if __name__ == "__main__": load_dotenv() import sentry_sdk -from fastapi import FastAPI, HTTPException, Request, status -from fastapi.exceptions import RequestValidationError +from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from logger import get_logger from middlewares.cors import add_cors_middleware @@ -40,24 +41,6 @@ async def http_exception_handler(_, exc): ) -# log more details about validation errors (422) -def handle_request_validation_error(app: FastAPI): - @app.exception_handler(RequestValidationError) - async def validation_exception_handler( - request: Request, exc: RequestValidationError - ): - exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") - logger.error(request, exc_str) - content = { - "status_code": status.HTTP_422_UNPROCESSABLE_ENTITY, - "message": exc_str, - "data": None, - } - return JSONResponse( - content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY - ) - - handle_request_validation_error(app) if __name__ == "__main__": diff --git a/backend/crawl_service.py b/backend/crawl_service.py index ec0c8c81f..929811d84 100644 --- a/backend/crawl_service.py +++ b/backend/crawl_service.py @@ -1,5 +1,7 @@ import os +from utils import handle_request_validation_error + if __name__ == "__main__": # import needed here when running main.py to debug backend # you will need to run pip install python-dotenv @@ -7,8 +9,7 @@ if __name__ == "__main__": load_dotenv() import sentry_sdk -from fastapi import FastAPI, HTTPException, Request, status -from fastapi.exceptions import RequestValidationError +from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from logger import get_logger from middlewares.cors import add_cors_middleware @@ -41,24 +42,6 @@ async def http_exception_handler(_, exc): ) -# log more details about validation errors (422) -def handle_request_validation_error(app: FastAPI): - @app.exception_handler(RequestValidationError) - async def validation_exception_handler( - request: Request, exc: RequestValidationError - ): - exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") - logger.error(request, exc_str) - content = { - "status_code": status.HTTP_422_UNPROCESSABLE_ENTITY, - "message": exc_str, - "data": None, - } - return JSONResponse( - content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY - ) - - handle_request_validation_error(app) if __name__ == "__main__": diff --git a/backend/main.py b/backend/main.py index 92008b7c4..970166ffc 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,5 +1,7 @@ import os +from utils import handle_request_validation_error + if __name__ == "__main__": # import needed here when running main.py to debug backend # you will need to run pip install python-dotenv @@ -8,8 +10,7 @@ if __name__ == "__main__": load_dotenv() import pypandoc import sentry_sdk -from fastapi import FastAPI, HTTPException, Request, status -from fastapi.exceptions import RequestValidationError +from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from logger import get_logger from middlewares.cors import add_cors_middleware @@ -30,8 +31,9 @@ from routes.contact_routes import router as contact_router logger = get_logger(__name__) -if (os.getenv("DEV_MODE") == "true"): +if os.getenv("DEV_MODE") == "true": import debugpy + logger.debug("👨‍💻 Running in dev mode") debugpy.listen(("0.0.0.0", 5678)) @@ -79,24 +81,6 @@ async def http_exception_handler(_, exc): ) -# log more details about validation errors (422) -def handle_request_validation_error(app: FastAPI): - @app.exception_handler(RequestValidationError) - async def validation_exception_handler( - request: Request, exc: RequestValidationError - ): - exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") - logger.error(request, exc_str) - content = { - "status_code": status.HTTP_422_UNPROCESSABLE_ENTITY, - "message": exc_str, - "data": None, - } - return JSONResponse( - content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY - ) - - handle_request_validation_error(app) if __name__ == "__main__": diff --git a/backend/upload_service.py b/backend/upload_service.py index badbc5831..c746e687b 100644 --- a/backend/upload_service.py +++ b/backend/upload_service.py @@ -1,5 +1,7 @@ import os +from utils import handle_request_validation_error + if __name__ == "__main__": # import needed here when running main.py to debug backend # you will need to run pip install python-dotenv @@ -8,8 +10,7 @@ if __name__ == "__main__": load_dotenv() import pypandoc import sentry_sdk -from fastapi import FastAPI, HTTPException, Request, status -from fastapi.exceptions import RequestValidationError +from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from logger import get_logger from middlewares.cors import add_cors_middleware @@ -49,24 +50,6 @@ async def http_exception_handler(_, exc): ) -# log more details about validation errors (422) -def handle_request_validation_error(app: FastAPI): - @app.exception_handler(RequestValidationError) - async def validation_exception_handler( - request: Request, exc: RequestValidationError - ): - exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") - logger.error(request, exc_str) - content = { - "status_code": status.HTTP_422_UNPROCESSABLE_ENTITY, - "message": exc_str, - "data": None, - } - return JSONResponse( - content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY - ) - - handle_request_validation_error(app) if __name__ == "__main__": diff --git a/backend/utils/__init__.py b/backend/utils/__init__.py index e69de29bb..208defb14 100644 --- a/backend/utils/__init__.py +++ b/backend/utils/__init__.py @@ -0,0 +1 @@ +from .handle_request_validation_error import handle_request_validation_error diff --git a/backend/utils/handle_request_validation_error.py b/backend/utils/handle_request_validation_error.py new file mode 100644 index 000000000..af7676109 --- /dev/null +++ b/backend/utils/handle_request_validation_error.py @@ -0,0 +1,23 @@ +from fastapi import FastAPI, Request, status +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from logger import get_logger + +logger = get_logger(__name__) + + +def handle_request_validation_error(app: FastAPI): + @app.exception_handler(RequestValidationError) + async def validation_exception_handler( + request: Request, exc: RequestValidationError + ): + exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") + logger.error(request, exc_str) + content = { + "status_code": status.HTTP_422_UNPROCESSABLE_ENTITY, + "message": exc_str, + "data": None, + } + return JSONResponse( + content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY + )