From abab9b359fa2d1bf0f9b25fe62a3d13e5b9e3440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Fri, 2 May 2025 04:28:03 +0200 Subject: [PATCH] Add custom 404 and 500 error handlers with corresponding templates --- core/templates/404.html | 14 ++++++++++++++ core/templates/500.html | 14 ++++++++++++++ core/urls.py | 4 ++-- core/views.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 core/templates/404.html create mode 100644 core/templates/500.html diff --git a/core/templates/404.html b/core/templates/404.html new file mode 100644 index 0000000..61ac621 --- /dev/null +++ b/core/templates/404.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block content %} +
+
+
+

404

+

Page Not Found

+

The page you're looking for doesn't exist or has been moved.

+

Check the URL or return to the homepage.

+
+
+
+{% endblock content %} diff --git a/core/templates/500.html b/core/templates/500.html new file mode 100644 index 0000000..37823b2 --- /dev/null +++ b/core/templates/500.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block content %} +
+
+
+

500

+

Internal Server Error

+

An unexpected error occurred while processing your request.

+

Check the URL or return to the homepage.

+
+
+
+{% endblock content %} diff --git a/core/urls.py b/core/urls.py index 7fd97db..641c4d3 100644 --- a/core/urls.py +++ b/core/urls.py @@ -8,8 +8,8 @@ from core.views import get_game, get_games, get_home app_name: str = "core" -# TODO(TheLovinator): Add a 404 page and a 500 page. -# https://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views +handler404 = "core.views.handler404" +handler500 = "core.views.handler500" # TODO(TheLovinator): Add a robots.txt file. # https://developers.google.com/search/docs/crawling-indexing/robots/intro diff --git a/core/views.py b/core/views.py index bff2dc4..ec6a213 100644 --- a/core/views.py +++ b/core/views.py @@ -19,6 +19,36 @@ if TYPE_CHECKING: logger: logging.Logger = logging.getLogger(__name__) +def handler404(request: HttpRequest, exception: Exception | None = None) -> HttpResponse: + """Custom 404 error handler. + + Args: + request (HttpRequest): The request object that caused the 404. + exception (Exception, optional): The exception that caused the 404. Defaults to None. + + Returns: + HttpResponse: The rendered 404 template. + """ + logger.warning( + "404 error occurred", + extra={"path": request.path, "exception": str(exception) if exception else None}, + ) + return render(request=request, template_name="404.html", status=404) + + +def handler500(request: HttpRequest) -> HttpResponse: + """Custom 500 error handler. + + Args: + request (HttpRequest): The request object that caused the 500. + + Returns: + HttpResponse: The rendered 500 template. + """ + logger.error("500 error occurred", extra={"path": request.path}) + return render(request=request, template_name="500.html", status=500) + + @require_http_methods(request_method_list=["GET", "HEAD"]) def get_home(request: HttpRequest) -> HttpResponse: """Render the index page with drops grouped hierarchically by game and campaign.