mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2026-02-05 02:35:18 -05:00
refactor: Replace direct logger.error calls with capture_and_log in accounts services and conditionally pass error_id during ApplicationError creation.
This commit is contained in:
@@ -26,6 +26,7 @@ from django.utils.crypto import get_random_string
|
|||||||
from django_forwardemail.services import EmailService
|
from django_forwardemail.services import EmailService
|
||||||
|
|
||||||
from .models import EmailVerification, User, UserDeletionRequest, UserProfile
|
from .models import EmailVerification, User, UserDeletionRequest, UserProfile
|
||||||
|
from apps.core.utils import capture_and_log
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -130,7 +131,7 @@ class AccountService:
|
|||||||
html=email_html,
|
html=email_html,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to send password change confirmation email: {e}")
|
capture_and_log(e, 'Send password change confirmation email', source='service', severity='medium')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def initiate_email_change(
|
def initiate_email_change(
|
||||||
@@ -206,7 +207,7 @@ class AccountService:
|
|||||||
html=email_html,
|
html=email_html,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to send email verification: {e}")
|
capture_and_log(e, 'Send email verification', source='service', severity='medium')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def verify_email_change(*, token: str) -> dict[str, Any]:
|
def verify_email_change(*, token: str) -> dict[str, Any]:
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ from django.utils import timezone
|
|||||||
from django_forwardemail.services import EmailService
|
from django_forwardemail.services import EmailService
|
||||||
|
|
||||||
from apps.accounts.models import NotificationPreference, User, UserNotification
|
from apps.accounts.models import NotificationPreference, User, UserNotification
|
||||||
|
from apps.core.utils import capture_and_log
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -264,7 +265,7 @@ class NotificationService:
|
|||||||
logger.info(f"Email notification sent to {user.email} for notification {notification.id}")
|
logger.info(f"Email notification sent to {user.email} for notification {notification.id}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to send email notification {notification.id}: {str(e)}")
|
capture_and_log(e, f'Send email notification {notification.id}', source='service')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_user_notifications(
|
def get_user_notifications(
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ if TYPE_CHECKING:
|
|||||||
else:
|
else:
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
from apps.core.utils import capture_and_log
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -62,7 +64,7 @@ class SocialProviderService:
|
|||||||
return True, "Provider can be safely disconnected."
|
return True, "Provider can be safely disconnected."
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error checking disconnect permission for user {user.id}, provider {provider}: {e}")
|
capture_and_log(e, f'Check disconnect permission for user {user.id}, provider {provider}', source='service')
|
||||||
return False, "Unable to verify disconnection safety. Please try again."
|
return False, "Unable to verify disconnection safety. Please try again."
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -97,7 +99,7 @@ class SocialProviderService:
|
|||||||
return connected_providers
|
return connected_providers
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting connected providers for user {user.id}: {e}")
|
capture_and_log(e, f'Get connected providers for user {user.id}', source='service')
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -140,7 +142,7 @@ class SocialProviderService:
|
|||||||
return available_providers
|
return available_providers
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting available providers: {e}")
|
capture_and_log(e, 'Get available providers', source='service')
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -177,7 +179,7 @@ class SocialProviderService:
|
|||||||
return True, f"{provider.title()} account disconnected successfully."
|
return True, f"{provider.title()} account disconnected successfully."
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error disconnecting {provider} for user {user.id}: {e}")
|
capture_and_log(e, f'Disconnect {provider} for user {user.id}', source='service')
|
||||||
return False, f"Failed to disconnect {provider} account. Please try again."
|
return False, f"Failed to disconnect {provider} account. Please try again."
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -210,7 +212,7 @@ class SocialProviderService:
|
|||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting auth status for user {user.id}: {e}")
|
capture_and_log(e, f'Get auth status for user {user.id}', source='service')
|
||||||
return {"error": "Unable to retrieve authentication status"}
|
return {"error": "Unable to retrieve authentication status"}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -236,5 +238,5 @@ class SocialProviderService:
|
|||||||
return True, f"Provider '{provider}' is valid and available."
|
return True, f"Provider '{provider}' is valid and available."
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error validating provider {provider}: {e}")
|
capture_and_log(e, f'Validate provider {provider}', source='service')
|
||||||
return False, "Unable to validate provider."
|
return False, "Unable to validate provider."
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ from django.db import transaction
|
|||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from apps.core.utils import capture_and_log
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
@@ -292,5 +294,5 @@ class UserDeletionService:
|
|||||||
logger.info(f"Deletion verification email sent to {user.email}")
|
logger.info(f"Deletion verification email sent to {user.email}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to send deletion verification email to {user.email}: {str(e)}")
|
capture_and_log(e, f'Send deletion verification email to {user.email}', source='service')
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -130,23 +130,28 @@ class ErrorService:
|
|||||||
# Merge request_context into metadata
|
# Merge request_context into metadata
|
||||||
merged_metadata = {**(metadata or {}), "request_context": request_context}
|
merged_metadata = {**(metadata or {}), "request_context": request_context}
|
||||||
|
|
||||||
|
# Build create kwargs, only including error_id if provided
|
||||||
|
create_kwargs = {
|
||||||
|
"error_type": error_type,
|
||||||
|
"error_message": error_message[:5000], # Limit message length
|
||||||
|
"error_stack": error_stack[:10000], # Limit stack length
|
||||||
|
"error_code": error_code,
|
||||||
|
"severity": severity,
|
||||||
|
"source": source,
|
||||||
|
"endpoint": endpoint,
|
||||||
|
"http_method": http_method,
|
||||||
|
"user_agent": user_agent[:1000],
|
||||||
|
"user": user,
|
||||||
|
"ip_address_hash": ip_address_hash,
|
||||||
|
"metadata": merged_metadata,
|
||||||
|
"environment": environment or {},
|
||||||
|
}
|
||||||
|
# Only include error_id if explicitly provided, else let model default
|
||||||
|
if error_id is not None:
|
||||||
|
create_kwargs["error_id"] = error_id
|
||||||
|
|
||||||
# Create and save error
|
# Create and save error
|
||||||
app_error = ApplicationError.objects.create(
|
app_error = ApplicationError.objects.create(**create_kwargs)
|
||||||
error_id=error_id or None, # Let model generate if not provided
|
|
||||||
error_type=error_type,
|
|
||||||
error_message=error_message[:5000], # Limit message length
|
|
||||||
error_stack=error_stack[:10000], # Limit stack length
|
|
||||||
error_code=error_code,
|
|
||||||
severity=severity,
|
|
||||||
source=source,
|
|
||||||
endpoint=endpoint,
|
|
||||||
http_method=http_method,
|
|
||||||
user_agent=user_agent[:1000],
|
|
||||||
user=user,
|
|
||||||
ip_address_hash=ip_address_hash,
|
|
||||||
metadata=merged_metadata,
|
|
||||||
environment=environment or {},
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Captured error {app_error.short_error_id}: {error_type} from {source}"
|
f"Captured error {app_error.short_error_id}: {error_type} from {source}"
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ from django.conf import settings
|
|||||||
from django.core.files.uploadedfile import UploadedFile
|
from django.core.files.uploadedfile import UploadedFile
|
||||||
from PIL import ExifTags, Image
|
from PIL import ExifTags, Image
|
||||||
|
|
||||||
|
from apps.core.utils import capture_and_log
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -193,5 +195,5 @@ class MediaService:
|
|||||||
"available_space": "unknown",
|
"available_space": "unknown",
|
||||||
}
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to get storage stats: {str(e)}")
|
capture_and_log(e, 'Get storage stats', source='service', severity='low')
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
|
|||||||
Reference in New Issue
Block a user