feat: Implement centralized error capture and handling with new middleware, services, and API endpoints, and add new admin and statistics API views.

This commit is contained in:
pacnpal
2026-01-02 15:55:42 -05:00
parent 1adba1b804
commit 95700c7d7b
43 changed files with 2477 additions and 158 deletions

View File

@@ -15,6 +15,7 @@ from rest_framework.serializers import Serializer
from rest_framework.views import APIView
from apps.api.v1.serializers.shared import validate_filter_metadata_contract
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__)
@@ -45,17 +46,12 @@ class ContractCompliantAPIView(APIView):
return response
except Exception as e:
# Log the error with context
logger.error(
f"API error in {self.__class__.__name__}: {str(e)}",
extra={
"view_class": self.__class__.__name__,
"request_path": request.path,
"request_method": request.method,
"user": getattr(request, "user", None),
"detail": str(e),
},
exc_info=True,
# Capture error to dashboard
capture_and_log(
e,
f'API error in {self.__class__.__name__}',
source='api',
severity='high',
)
# Return standardized error response
@@ -194,10 +190,10 @@ class FilterMetadataAPIView(ContractCompliantAPIView):
return self.success_response(validated_metadata)
except Exception as e:
logger.error(
f"Error getting filter metadata in {self.__class__.__name__}: {str(e)}",
extra={"view_class": self.__class__.__name__, "detail": str(e)},
exc_info=True,
capture_and_log(
e,
f'Get filter metadata in {self.__class__.__name__}',
source='api',
)
return self.error_response(message="Failed to retrieve filter metadata", error_code="FILTER_METADATA_ERROR")
@@ -238,14 +234,10 @@ class HybridFilteringAPIView(ContractCompliantAPIView):
return self.success_response(hybrid_data)
except Exception as e:
logger.error(
f"Error in hybrid filtering for {self.__class__.__name__}: {str(e)}",
extra={
"view_class": self.__class__.__name__,
"filters": getattr(self, "_extracted_filters", {}),
"detail": str(e),
},
exc_info=True,
capture_and_log(
e,
f'Hybrid filtering for {self.__class__.__name__}',
source='api',
)
return self.error_response(message="Failed to retrieve filtered data", error_code="HYBRID_FILTERING_ERROR")
@@ -392,7 +384,7 @@ def contract_compliant_view(view_class):
return response
except Exception as e:
logger.error(f"Error in decorated view {view_class.__name__}: {str(e)}", exc_info=True)
capture_and_log(e, f'Decorated view {view_class.__name__}', source='api')
# Return basic error response
return Response(