Skip to content

Commit

Permalink
feat: add custom error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dnikolay-ebc committed Jul 14, 2022
1 parent a556bdd commit a84053a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/Entirety/entirety/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

from . import views

handler404 = "entirety.views.custom_page_not_found_view"
handler500 = "entirety.views.custom_error_view"
handler403 = "entirety.views.custom_permission_denied_view"
handler400 = "entirety.views.custom_bad_request_view"

urlpatterns = [
path("admin/", admin.site.urls),
path("", views.home, name="home"),
Expand Down
23 changes: 23 additions & 0 deletions app/Entirety/entirety/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import requests

from django.shortcuts import render


def home(request):
context = {}
return render(request, "home.html", context)


def __generic_error_handler(request, status_code: int):
context = {"error_code": status_code}
return render(request, "error.html", context)


def custom_page_not_found_view(request, *args, **kwargs):
return __generic_error_handler(request, status_code=404)


def custom_error_view(request, *args, **kwargs):
return __generic_error_handler(request, status_code=500)


def custom_permission_denied_view(request, *args, **kwargs):
return __generic_error_handler(request, status_code=403)


def custom_bad_request_view(request, *args, **kwargs):
return __generic_error_handler(request, status_code=400)
10 changes: 10 additions & 0 deletions app/Entirety/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends '_base.html' %}

{% block title %}Error {{ error_code }}{% endblock %}

{% block content %}
<div class="d-flex">
{# TODO: use custom error layout, but hey cats :D #}
<img src="https://http.cat/{{ error_code }}.jpg" class="rounded mx-auto d-block" alt="...">
</div>
{% endblock %}

0 comments on commit a84053a

Please sign in to comment.