<commit_before>from aiohttp import web
from mako.template import Template

from virtool.utils import get_static_hash


template_500 = Template(filename="virtool/templates/error_500.html")


async def middleware_factory(app, handler):
    async def middleware_handler(request):
        try:
            response = await handler(request)

            if not request.path.startswith("/api"):
                if response.status == 404:
                    return handle_404()

            return response

        except web.HTTPException as ex:
            if ex.status == 404:
                return handle_404()

            raise

    return middleware_handler


def handle_404():
    html = Template(filename="virtool/templates/error_404.html").render(hash=get_static_hash())
    return web.Response(body=html, content_type="text/html")
<commit_msg>Make HTTPExceptions return errors for /api calls<commit_after>from aiohttp import web
from mako.template import Template

from virtool.utils import get_static_hash


template_500 = Template(filename="virtool/templates/error_500.html")


async def middleware_factory(app, handler):
    async def middleware_handler(request):
        is_api_call = request.path.startswith("/api")

        try:
            response = await handler(request)

            if not is_api_call:
                if response.status == 404:
                    return handle_404()

            return response

        except web.HTTPException as ex:
            if not is_api_call and ex.status == 404:
                return handle_404()

            raise

    return middleware_handler


def handle_404():
    html = Template(filename="virtool/templates/error_404.html").render(hash=get_static_hash())
    return web.Response(body=html, content_type="text/html")

{'fraction_non_alphanumeric': 0.08379888268156424, 'fraction_numerical': 0.026815642458100558, 'mean_word_length': 2.6776180698151952, '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': '5180440', 'n_tokens_mistral': 528, 'n_tokens_neox': 469, 'n_words': 119}