aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/errors/http/http_4xx_errors.py
blob: 704f11bff323dcb7516e45eb67f95d9cf8bc828a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Handlers for HTTP 4** errors"""
import logging
from werkzeug.exceptions import NotFound

from flask import request, jsonify, render_template

from gn_auth.errors.tracing import add_trace

__all__ = ["http_4xx_error_handlers"]

logger = logging.getLogger(__name__)

def page_not_found(exc):
    """404 handler."""
    logger.error("Page '%s' was not found.", request.url, exc_info=True)
    content_type = request.content_type
    if bool(content_type) and content_type.lower() == "application/json":
        return jsonify(add_trace(exc, {
            "error": exc.name,
            "error_description": (f"The page '{request.url}' does not exist on "
                                  "this server.")
        })), exc.code

    return render_template("404.html", page=request.url), exc.code


def http_4xx_error_handlers() -> dict:
    """Return handlers for HTTP errors in the 400-499 range"""
    return {
        NotFound: page_not_found
    }