/
error_handler.py
131 lines (108 loc) · 3.69 KB
/
error_handler.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from django.conf import settings
from django.http import (
HttpResponseBadRequest,
HttpResponseForbidden,
HttpResponseNotFound,
HttpResponseServerError,
)
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
if TYPE_CHECKING:
from typing import Any
from django.core.exceptions import BadRequest, Http404, PermissionDenied
from django.http import HttpRequest
from django.utils.safestring import SafeString
logger = logging.getLogger(__name__)
def render_error_template(context: dict[str, Any]) -> SafeString:
"""
Render the HTTP error template
:param context: The context data for the error template
:return: The rendered template response
"""
context.update(
{
"COMPANY": settings.COMPANY,
"COMPANY_URL": settings.COMPANY_URL,
"BRANDING": settings.BRANDING,
"BRANDING_TITLE": settings.BRANDING_TITLE,
}
)
return render_to_string("error_handler/http_error.html", context)
def handler400(request: HttpRequest, exception: BadRequest) -> HttpResponseBadRequest:
"""
Render a HTTP 400 Error code
:param request: Object representing the user call
:param exception: Exception (unused)
:return: The rendered template response
"""
context = {
"request": request,
"code": 400,
"title": _("Bad request"),
"message": _("There was an error in your request."),
}
logger.debug(exception)
return HttpResponseBadRequest(render_error_template(context))
def handler403(
request: HttpRequest, exception: PermissionDenied
) -> HttpResponseForbidden:
"""
Render a HTTP 403 Error code
:param request: Object representing the user call
:param exception: Exception (unused)
:return: The rendered template response
"""
context = {
"request": request,
"code": 403,
"title": _("Forbidden"),
"message": _("You don't have the permission to access this page."),
}
logger.debug(exception)
return HttpResponseForbidden(render_error_template(context))
def handler404(request: HttpRequest, exception: Http404) -> HttpResponseNotFound:
"""
Render a HTTP 404 Error code
:param request: Object representing the user call
:param exception: Exception (unused)
:return: The rendered template response
"""
context = {
"request": request,
"code": 404,
"title": _("Page not found"),
"message": _("The page you requested could not be found."),
}
logger.debug(exception)
return HttpResponseNotFound(render_error_template(context))
def handler500(request: HttpRequest) -> HttpResponseServerError:
"""
Render a HTTP 500 Error code
:param request: Object representing the user call
:return: The rendered template response
"""
context = {
"request": request,
"code": 500,
"title": _("Internal Server Error"),
"message": _("An unexpected error has occurred."),
}
return HttpResponseServerError(render_error_template(context))
def csrf_failure(request: HttpRequest, reason: str) -> HttpResponseForbidden:
"""
Render a CSRF failure notice
:param request: Object representing the user call
:param reason: Description of reason for CSRF failure
:return: The rendered template response
"""
context = {
"request": request,
"code": 403,
"title": _("CSRF Error"),
"message": _("Please try to reload the page."),
}
logger.debug(reason)
return HttpResponseForbidden(render_error_template(context))