<commit_before># -*- coding: utf-8 -*-
# This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import

import sys
import traceback

from django.conf import settings
from django.http import HttpResponseServerError
from django.shortcuts import render
from django.template import TemplateDoesNotExist
from django.utils.deprecation import MiddlewareMixin
from shibboleth.middleware import ShibbolethRemoteUserMiddleware

import elasticsearch


class AJAXSimpleExceptionResponseMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        if not settings.DEBUG or not request.is_ajax():
            return
        (exc_type, exc_info, tb) = sys.exc_info()
        response = "%s\n" % exc_type.__name__
        response += "%s\n\n" % exc_info
        response += "TRACEBACK:\n"
        for tb in traceback.format_tb(tb):
            response += "%s\n" % tb
        return HttpResponseServerError(response)


class SpecificExceptionErrorPageResponseMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        if settings.DEBUG and isinstance(exception, TemplateDoesNotExist):
            return HttpResponseServerError("Missing template: " + str(exception))


class ElasticsearchMiddleware(MiddlewareMixin):
    """
    Redirect the user to a friendly error page when an exception related to
    Elasticsearch is detected.

    The goal is to inform the user that the error is related to Elasticsearch
    when they are running in production, as this seems to be a common problem,
    mainly because users frequently experience Elasticsearch node crashes when
    running them with not enough memory.
    """

    EXCEPTIONS = (
        elasticsearch.ElasticsearchException,<commit_msg>Add logger to log exceptions in AJAX middleware<commit_after># -*- coding: utf-8 -*-
# This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import

import sys
import traceback
import logging

from django.conf import settings
from django.http import HttpResponseServerError
from django.shortcuts import render
from django.template import TemplateDoesNotExist
from django.utils.deprecation import MiddlewareMixin
from shibboleth.middleware import ShibbolethRemoteUserMiddleware

import elasticsearch


logger = logging.getLogger("archivematica.dashboard")


class AJAXSimpleExceptionResponseMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        if not settings.DEBUG or not request.is_ajax():
            return
        (exc_type, exc_info, tb) = sys.exc_info()
        tracebacks = traceback.format_tb(tb)
        logger.error(
            "Processing exception %s at %s. Traceback %s",
            exception,
            request.path,
            tracebacks,
        )
        response = "%s\n" % exc_type.__name__
        response += "%s\n\n" % exc_info
        response += "TRACEBACK:\n"
        for tb in tracebacks:
            response += "%s\n" % tb
        return HttpResponseServerError(response)


class SpecificExceptionErrorPageResponseMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        if settings.DEBUG and isinstance(exception, TemplateDoesNotExist):
            return HttpResponseServerError("Missing template: " + str(exception))


class ElasticsearchMiddleware(MiddlewareMixin):
    """
    Redirect the user to a friendly error page when an exception related to
    Elasticsearch is detected.

    The goal is to inform the user that the error is related to Elasticsearch
    when they are running in production, as this seems to be a common problem,
    mainly because users frequently experience Elasticsearch node crashes when
    running them with not enough memory.
    """

    EXCEPTIONS = (
        elasticsearch.ElasticsearchException,
{'fraction_non_alphanumeric': 0.0652049571020019, 'fraction_numerical': 0.0038131553860819827, 'mean_word_length': 3.756119673617407, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2963398', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1324, 'n_words': 539}