diff --git a/backend/backend/exception_handler.py b/backend/backend/exception_handler.py new file mode 100644 index 000000000..aeaca091b --- /dev/null +++ b/backend/backend/exception_handler.py @@ -0,0 +1,21 @@ +import logging +from typing import Any + +from rest_framework.response import Response +from rest_framework.views import exception_handler + +logger = logging.getLogger(__name__) + + +def bad_request_logger(exception: Any, context: dict[str, Any]) -> Response | None: + # Get the DRF exception handler standard error response + response = exception_handler(exception, context) + + if response is not None: + logger.warning( + f"Bad request: {context['request'].path} -" + f"Status Code: {response.status_code} -" + f"Data: {response.data} -" + ) + + return response diff --git a/backend/backend/settings.py b/backend/backend/settings.py index fd7f1c371..1d9f8f501 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -181,6 +181,7 @@ "DEFAULT_AUTHENTICATION_CLASSES": [ "rest_framework.authentication.TokenAuthentication", ], + "EXCEPTION_HANDLER": "backend.exception_handler.bad_request_logger", } SPECTACULAR_SETTINGS = {