<commit_before>from django.conf import settings
from django.utils.html import strip_tags
from django.utils.text import unescape_entities
from django.utils.encoding import force_unicode

ROA_DJANGO_ERRORS = getattr(settings, 'ROA_DJANGO_ERRORS', False)

class ROAException(Exception):
    def __init__(self, exception):
        if ROA_DJANGO_ERRORS:
            self.msg = exception.message
            self.status_code = exception.status_code
        else:
            self.msg = force_unicode(exception)

    def __str__(self):
        if ROA_DJANGO_ERRORS and '<body>' in self.msg:
            return self.parse_django_error()
        return self.msg
    
    def parse_django_error(self):
        """Extract the summary part of a Django HTML error."""
        summary = self.msg.split('<body>\n<div id="summary">\n  ', 1)[1]\
                          .split('<th>Python Executable:</th>', 1)[0]
        result = []
        title = None
        for line in strip_tags(summary).split('\n'):
            line_content = unescape_entities(line.strip())
            if line_content:
                if line_content.endswith(':'):
                    title = line_content
                elif title is None:
                    title = "%s:" % line_content
                else:
                    result.append("%s %s\n" % (title, line_content))
        result.append("Status code: %s" % self.status_code)
        return u" ".join([force_unicode(line) for line in result])<commit_msg>Better (Django's) errors feedback, ease debugging<commit_after>from django.conf import settings
from django.utils.html import strip_tags
from django.utils.text import unescape_entities
from django.utils.encoding import force_unicode

ROA_DJANGO_ERRORS = getattr(settings, 'ROA_DJANGO_ERRORS', False)

class ROAException(Exception):
    def __init__(self, exception):
        if ROA_DJANGO_ERRORS and 'message' in exception \
                             and 'status_code' in exception:
            self.msg = force_unicode(exception.message)
            self.status_code = exception.status_code
        else:
            self.msg = force_unicode(exception)
            self.status_code = 'XXX'

    def __str__(self):
        if ROA_DJANGO_ERRORS and '<body>' in self.msg:
            return self.parse_django_error()
        return self.msg
    
    def parse_django_error(self):
        """Extract the summary part of a Django HTML error."""
        summary = self.msg.split(u'<body>\n<div id="summary">\n  ', 1)[1]\
                          .split(u'<th>Python Executable:</th>', 1)[0]
        traceback = self.msg.split(u'\n\nTraceback:', 1)[1]\
                            .split(u'</textarea>', 1)[0]
        result = []
        title = None
        for line in strip_tags(summary).split('\n'):
            line_content = unescape_entities(line.strip())
            if line_content:
                if line_content.endswith(':'):
                    title = line_content
                elif title is None:
                    title = "%s:" % line_content
                else:
                    result.append("%s %s\n" % (title, line_content))
        result.append("Status code: %s" % self.status_code)
        indent, indent2 = u'  ', u'    '
        return u"%(summary)s %(traceback)s".strip() % {
            'summary': indent.join(force_unicode(line) for line in result),
            'traceback': indent2.join(force_unicode(line+"\n") \
                                        for line in traceback.split('\n')),
        }
{'fraction_non_alphanumeric': 0.11335227272727273, 'fraction_numerical': 0.003977272727272727, 'mean_word_length': 2.075109170305677, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6408671', 'n_tokens_mistral': 1031, 'n_tokens_neox': 996, 'n_words': 235}