<commit_before>from django import http
from django.template import RequestContext, loader
from django.http import HttpResponse
from django.conf import settings

from meta.alerts import local_red_alert

class ServerErrorMiddleware(object):
    def get_most_recent_line_in_traceback_from_what_codebase(self, traceback_string):
        traceback_lines = traceback_string.split('\n')
        for line in traceback_lines:
            if len(line.split(settings.PROJECT_ROOT)) > 1: #See if we can split the string by the project root directory.
                #...if so, this is the winner.
                return line
    
    def get_useful_info_from_traceback_line(self, line):
        remainder_of_the_line = line.split(settings.PROJECT_ROOT)[1] #The part of the line that comes after PROJECT ROOT
        file = remainder_of_the_line.split('"')[0]
        other_useful_info = remainder_of_the_line.split('"')[1][2:]
        return file, other_useful_info

    def process_exception(self, request, exception):
        traceback = self._get_traceback()
        line = self.get_most_recent_line_in_traceback_from_what_codebase(traceback)
        file, other_useful_info = self.get_useful_info_from_traceback_line(line)
        
        #Now we have the useful info.  We need to tell the world.
        message = "A critical error has occurred in production. %s.  Likely culprit: %s on or near %s" % (str(exception), file, other_useful_info)
        local_red_alert(message) #Raise the alarm!
        return None #Use default exception handling.
    
    
        
    def _get_traceback(self):    
        """
        Helper function to return the traceback as a string
        From django snippet 638 by kcarnold
        """
        import traceback
        import sys
        return '\n'.join(traceback.format_exception(*(sys.exc_info())))

def page_not_found(request):
    t = loader.get_template('errors/404.html')
    return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))

def server_error(request):
    pass
    return HttpResponse('Llamas')<commit_msg>Fixed #15 - alert phone calls for 500 only dispatched if DEBUG is True.<commit_after>from django import http
from django.template import RequestContext, loader
from django.http import HttpResponse
from django.conf import settings

from meta.alerts import local_red_alert

class ServerErrorMiddleware(object):
    def get_most_recent_line_in_traceback_from_what_codebase(self, traceback_string):
        traceback_lines = traceback_string.split('\n')
        for line in traceback_lines:
            if len(line.split(settings.PROJECT_ROOT)) > 1: #See if we can split the string by the project root directory.
                #...if so, this is the winner.
                return line
    
    def get_useful_info_from_traceback_line(self, line):
        remainder_of_the_line = line.split(settings.PROJECT_ROOT)[1] #The part of the line that comes after PROJECT ROOT
        file = remainder_of_the_line.split('"')[0]
        other_useful_info = remainder_of_the_line.split('"')[1][2:]
        return file, other_useful_info

    def process_exception(self, request, exception):
        if not settings.DEBUG:
            traceback = self._get_traceback()
            line = self.get_most_recent_line_in_traceback_from_what_codebase(traceback)
            file, other_useful_info = self.get_useful_info_from_traceback_line(line)
            
            #Now we have the useful info.  We need to tell the world.
            message = "A critical error has occurred in production. %s.  Likely culprit: %s on or near %s" % (str(exception), file, other_useful_info)
            local_red_alert(message) #Raise the alarm!
        return None #Use default exception handling.
    
    
        
    def _get_traceback(self):    
        """
        Helper function to return the traceback as a string
        From django snippet 638 by kcarnold
        """
        import traceback
        import sys
        return '\n'.join(traceback.format_exception(*(sys.exc_info())))

def page_not_found(request):
    t = loader.get_template('errors/404.html')
    return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))

def server_error(request):
    pass
    return HttpResponse('Llamas')
{'fraction_non_alphanumeric': 0.08123119648229576, 'fraction_numerical': 0.006248553575561213, 'mean_word_length': 3.1557692307692307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1260743', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1250, 'n_words': 344}