authentik.core.views.error

authentik core error views

 1"""authentik core error views"""
 2
 3from django.http.response import (
 4    HttpResponseBadRequest,
 5    HttpResponseForbidden,
 6    HttpResponseNotFound,
 7    HttpResponseServerError,
 8)
 9from django.template.response import TemplateResponse
10from django.views.generic import TemplateView
11
12
13class BadRequestTemplateResponse(TemplateResponse, HttpResponseBadRequest):
14    """Combine Template response with Http Code 400"""
15
16
17class ForbiddenTemplateResponse(TemplateResponse, HttpResponseForbidden):
18    """Combine Template response with Http Code 403"""
19
20
21class NotFoundTemplateResponse(TemplateResponse, HttpResponseNotFound):
22    """Combine Template response with Http Code 404"""
23
24
25class ServerErrorTemplateResponse(TemplateResponse, HttpResponseServerError):
26    """Combine Template response with Http Code 500"""
27
28
29class ErrorView(TemplateView):
30    """Base for the views wired up as django's handler400/403/404/500.
31
32    Django invokes an error handler with the request that failed, whatever its
33    method. A plain TemplateView only implements `get`, so its `dispatch` answers
34    a failing POST/PUT/PATCH/DELETE with 405 Method Not Allowed and discards the
35    handler's own status code -- which is how an API error surfaced as a 405 with
36    an `Allow: GET, HEAD, OPTIONS` header. Render the page for every method so the
37    status code the handler stands for is the one the client sees.
38    """
39
40    template_name = "if/error.html"
41
42    def dispatch(self, request, *args, **kwargs):
43        return self.get(request, *args, **kwargs)
44
45
46class BadRequestView(ErrorView):
47    """Show Bad Request message"""
48
49    extra_context = {"title": "Bad Request"}
50
51    response_class = BadRequestTemplateResponse
52
53
54class ForbiddenView(ErrorView):
55    """Show Forbidden message"""
56
57    extra_context = {"title": "Forbidden"}
58
59    response_class = ForbiddenTemplateResponse
60
61
62class NotFoundView(ErrorView):
63    """Show Not Found message"""
64
65    extra_context = {"title": "Not Found"}
66
67    response_class = NotFoundTemplateResponse
68
69
70class ServerErrorView(ErrorView):
71    """Show Server Error message"""
72
73    extra_context = {"title": "Server Error"}
74
75    response_class = ServerErrorTemplateResponse
class BadRequestTemplateResponse(django.template.response.TemplateResponse, django.http.response.HttpResponseBadRequest):
14class BadRequestTemplateResponse(TemplateResponse, HttpResponseBadRequest):
15    """Combine Template response with Http Code 400"""

Combine Template response with Http Code 400

class ForbiddenTemplateResponse(django.template.response.TemplateResponse, django.http.response.HttpResponseForbidden):
18class ForbiddenTemplateResponse(TemplateResponse, HttpResponseForbidden):
19    """Combine Template response with Http Code 403"""

Combine Template response with Http Code 403

class NotFoundTemplateResponse(django.template.response.TemplateResponse, django.http.response.HttpResponseNotFound):
22class NotFoundTemplateResponse(TemplateResponse, HttpResponseNotFound):
23    """Combine Template response with Http Code 404"""

Combine Template response with Http Code 404

class ServerErrorTemplateResponse(django.template.response.TemplateResponse, django.http.response.HttpResponseServerError):
26class ServerErrorTemplateResponse(TemplateResponse, HttpResponseServerError):
27    """Combine Template response with Http Code 500"""

Combine Template response with Http Code 500

class ErrorView(django.views.generic.base.TemplateView):
30class ErrorView(TemplateView):
31    """Base for the views wired up as django's handler400/403/404/500.
32
33    Django invokes an error handler with the request that failed, whatever its
34    method. A plain TemplateView only implements `get`, so its `dispatch` answers
35    a failing POST/PUT/PATCH/DELETE with 405 Method Not Allowed and discards the
36    handler's own status code -- which is how an API error surfaced as a 405 with
37    an `Allow: GET, HEAD, OPTIONS` header. Render the page for every method so the
38    status code the handler stands for is the one the client sees.
39    """
40
41    template_name = "if/error.html"
42
43    def dispatch(self, request, *args, **kwargs):
44        return self.get(request, *args, **kwargs)

Base for the views wired up as django's handler400/403/404/500.

Django invokes an error handler with the request that failed, whatever its method. A plain TemplateView only implements get, so its dispatch answers a failing POST/PUT/PATCH/DELETE with 405 Method Not Allowed and discards the handler's own status code -- which is how an API error surfaced as a 405 with an Allow: GET, HEAD, OPTIONS header. Render the page for every method so the status code the handler stands for is the one the client sees.

template_name = 'if/error.html'
def dispatch(self, request, *args, **kwargs):
43    def dispatch(self, request, *args, **kwargs):
44        return self.get(request, *args, **kwargs)
class BadRequestView(ErrorView):
47class BadRequestView(ErrorView):
48    """Show Bad Request message"""
49
50    extra_context = {"title": "Bad Request"}
51
52    response_class = BadRequestTemplateResponse

Show Bad Request message

extra_context = {'title': 'Bad Request'}
response_class = <class 'BadRequestTemplateResponse'>
Inherited Members
ErrorView
template_name
dispatch
class ForbiddenView(ErrorView):
55class ForbiddenView(ErrorView):
56    """Show Forbidden message"""
57
58    extra_context = {"title": "Forbidden"}
59
60    response_class = ForbiddenTemplateResponse

Show Forbidden message

extra_context = {'title': 'Forbidden'}
response_class = <class 'ForbiddenTemplateResponse'>
Inherited Members
ErrorView
template_name
dispatch
class NotFoundView(ErrorView):
63class NotFoundView(ErrorView):
64    """Show Not Found message"""
65
66    extra_context = {"title": "Not Found"}
67
68    response_class = NotFoundTemplateResponse

Show Not Found message

extra_context = {'title': 'Not Found'}
response_class = <class 'NotFoundTemplateResponse'>
Inherited Members
ErrorView
template_name
dispatch
class ServerErrorView(ErrorView):
71class ServerErrorView(ErrorView):
72    """Show Server Error message"""
73
74    extra_context = {"title": "Server Error"}
75
76    response_class = ServerErrorTemplateResponse

Show Server Error message

extra_context = {'title': 'Server Error'}
response_class = <class 'ServerErrorTemplateResponse'>
Inherited Members
ErrorView
template_name
dispatch