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

@@ -12,6 +12,7 @@ from django.core.files.uploadedfile import UploadedFile
from django.db import transaction
from apps.core.services.media_service import MediaService
from apps.core.utils import capture_and_log
from ..models import Ride, RidePhoto
@@ -190,7 +191,7 @@ class RideMediaService:
logger.info(f"Photo {photo.pk} approved by user {approved_by.username}")
return True
except Exception as e:
logger.error(f"Failed to approve photo {photo.pk}: {str(e)}")
capture_and_log(e, f'Approve ride photo {photo.pk}', source='service')
return False
@staticmethod
@@ -217,7 +218,7 @@ class RideMediaService:
logger.info(f"Photo {photo_id} deleted from ride {ride_slug} by user {deleted_by.username}")
return True
except Exception as e:
logger.error(f"Failed to delete photo {photo.pk}: {str(e)}")
capture_and_log(e, f'Delete ride photo {photo.pk}', source='service')
return False
@staticmethod

View File

@@ -21,6 +21,7 @@ from apps.rides.models import (
RideRanking,
RideReview,
)
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__)
@@ -96,7 +97,7 @@ class RideRankingService:
}
except Exception as e:
self.logger.error(f"Error updating rankings: {e}", exc_info=True)
capture_and_log(e, 'Update ride rankings', source='service')
raise
def _get_eligible_rides(self, category: str | None = None) -> list[Ride]:

View File

@@ -6,6 +6,7 @@ Following Django styleguide pattern for business logic encapsulation.
from django.contrib.auth.models import AbstractBaseUser
from django.db import transaction
from apps.core.utils import capture_and_log
from apps.rides.models import Ride
@@ -191,14 +192,8 @@ class RideStatusService:
ride.apply_post_closing_status()
transitioned_rides.append(ride)
except Exception as e:
# Log error but continue processing other rides
import logging
logger = logging.getLogger(__name__)
logger.error(
f"Failed to process closing ride {ride.id}: {e}",
exc_info=True,
)
# Capture error to dashboard but continue processing other rides
capture_and_log(e, f'Process closing ride {ride.id}', source='service')
continue
return transitioned_rides

View File

@@ -4,6 +4,8 @@ from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django.utils import timezone
from apps.core.utils import capture_and_log
from .models import Ride
logger = logging.getLogger(__name__)
@@ -126,7 +128,13 @@ def handle_ride_transition_to_closing(instance, source, target, user, **kwargs):
return True
if not instance.post_closing_status:
logger.error(f"Cannot transition ride {instance.pk} to CLOSING: " "post_closing_status not set")
# Capture to dashboard as a validation error
capture_and_log(
ValueError('post_closing_status not set for CLOSING transition'),
f'Ride transition to CLOSING for ride {instance.pk}',
source='signal',
severity='medium',
)
return False
if not instance.closing_date:

View File

@@ -12,6 +12,8 @@ from django.contrib.auth import get_user_model
from django.db import transaction
from django.utils import timezone
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__)
User = get_user_model()
@@ -59,12 +61,10 @@ def check_overdue_closings() -> dict:
failed += 1
error_msg = f"Ride {ride.id} ({ride.name}): {str(e)}"
failures.append(error_msg)
logger.error(
"Failed to transition ride %s (%s): %s",
ride.id,
ride.name,
str(e),
exc_info=True,
capture_and_log(
e,
f'Transition closing ride {ride.id} ({ride.name})',
source='task',
)
result = {