<commit_before>import traceback
from celery.utils.mail import ErrorMail
from django.core import mail
from django.utils.log import AdminEmailHandler
from django.views.debug import get_exception_reporter_filter
from django.template.loader import render_to_string


class HqAdminEmailHandler(AdminEmailHandler):
    """
    Custom AdminEmailHandler to include additional details which can be supplied as follows:

    logger.error(message,
        extra={
            'details': {'domain': 'demo', 'user': 'user1'}
        }
    )
    """

    def emit(self, record):
        # avoid circular dependency
        from django.conf import settings

        try:
            request = record.request
            subject = '%s (%s IP): %s' % (
                record.levelname,
                (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
                 and 'internal' or 'EXTERNAL'),
                record.getMessage()
            )
            filter = get_exception_reporter_filter(request)
            request_repr = filter.get_request_repr(request)
        except Exception:
            subject = '%s: %s' % (
                record.levelname,
                record.getMessage()
            )
            request = None
            request_repr = "Request repr() unavailable."
        subject = self.format_subject(subject)

        tb_list = []
        if record.exc_info:
            exc_info = record.exc_info
            etype, value, tb = exc_info
            tb_list = ['Traceback (most recent call first):\n']
            tb_list.extend(traceback.format_exception_only(etype, value))
            tb_list.extend(traceback.format_list(reversed(traceback.extract_tb(tb))))
            stack_trace = '\n'.join(tb_list)
        else:
            stack_trace = 'No stack trace available'

        message = "%s\n\n%s" % (stack_trace, request_repr)
        details = getattr(record, 'details', None)
        if details:
            message = "%s\n\n%s" % (self.format_details(details), message)

        context = {
            'details': details,
            'tb_list': tb_list,
            'get': request.GET,
            'post': request.POST,
            'method': request.method,
            'url': request.build_absolute_uri(),
            'request_repr': request_repr
        }
        html_message = render_to_string('hqadmin/email/error_email.html', context)
        mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)
<commit_msg>don't fail hard if request is None<commit_after>from collections import defaultdict
import traceback
from celery.utils.mail import ErrorMail
from django.core import mail
from django.utils.log import AdminEmailHandler
from django.views.debug import get_exception_reporter_filter
from django.template.loader import render_to_string


class HqAdminEmailHandler(AdminEmailHandler):
    """
    Custom AdminEmailHandler to include additional details which can be supplied as follows:

    logger.error(message,
        extra={
            'details': {'domain': 'demo', 'user': 'user1'}
        }
    )
    """

    def emit(self, record):
        # avoid circular dependency
        from django.conf import settings

        try:
            request = record.request
            subject = '%s (%s IP): %s' % (
                record.levelname,
                (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
                 and 'internal' or 'EXTERNAL'),
                record.getMessage()
            )
            filter = get_exception_reporter_filter(request)
            request_repr = filter.get_request_repr(request)
            raise Exception('foo')
        except Exception:
            subject = '%s: %s' % (
                record.levelname,
                record.getMessage()
            )
            request = None
            request_repr = "Request repr() unavailable."
        subject = self.format_subject(subject)

        tb_list = []
        if record.exc_info:
            exc_info = record.exc_info
            etype, value, tb = exc_info
            tb_list = ['Traceback (most recent call first):\n']
            tb_list.extend(traceback.format_exception_only(etype, value))
            tb_list.extend(traceback.format_list(reversed(traceback.extract_tb(tb))))
            stack_trace = '\n'.join(tb_list)
        else:
            stack_trace = 'No stack trace available'

        message = "%s\n\n%s" % (stack_trace, request_repr)
        details = getattr(record, 'details', None)
        if details:
            message = "%s\n\n%s" % (self.format_details(details), message)

        context = defaultdict(lambda: '')
        context.update({
            'details': details,
            'tb_list': tb_list,
            'request_repr': request_repr
        })
        if request:
            context.update({
                'get': request.GET,
                'post': request.POST,
                'method': request.method,
                'url': request.build_absolute_uri(),
            })
        html_message = render_to_string('hqadmin/email/error_email.html', context)
        mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)

{'fraction_non_alphanumeric': 0.10084843810258388, 'fraction_numerical': 0.0003856536829926726, 'mean_word_length': 2.085663295657347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7085453', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1395, 'n_words': 319}