Add custom 404 and 500 error handlers with corresponding templates
All checks were successful
Ruff / ruff (push) Successful in 10s

This commit is contained in:
2025-05-02 04:28:03 +02:00
parent f5a874c6df
commit abab9b359f
4 changed files with 60 additions and 2 deletions

14
core/templates/404.html Normal file
View File

@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block content %}
<div class="row justify-content-center">
<div class="col-md-8 text-center">
<div class="alert-danger mt-5">
<h1 class="display-1">404</h1>
<h2>Page Not Found</h2>
<p class="lead">The page you're looking for doesn't exist or has been moved.</p>
<p>Check the URL or return to the <a href="{% url 'index' %}" class="alert-link">homepage</a>.</p>
</div>
</div>
</div>
{% endblock content %}

14
core/templates/500.html Normal file
View File

@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block content %}
<div class="row justify-content-center">
<div class="col-md-8 text-center">
<div class="alert-danger mt-5">
<h1 class="display-1">500</h1>
<h2>Internal Server Error</h2>
<p class="lead">An unexpected error occurred while processing your request.</p>
<p>Check the URL or return to the <a href="{% url 'index' %}" class="alert-link">homepage</a>.</p>
</div>
</div>
</div>
{% endblock content %}

View File

@ -8,8 +8,8 @@ from core.views import get_game, get_games, get_home
app_name: str = "core" app_name: str = "core"
# TODO(TheLovinator): Add a 404 page and a 500 page. handler404 = "core.views.handler404"
# https://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views handler500 = "core.views.handler500"
# TODO(TheLovinator): Add a robots.txt file. # TODO(TheLovinator): Add a robots.txt file.
# https://developers.google.com/search/docs/crawling-indexing/robots/intro # https://developers.google.com/search/docs/crawling-indexing/robots/intro

View File

@ -19,6 +19,36 @@ if TYPE_CHECKING:
logger: logging.Logger = logging.getLogger(__name__) 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"]) @require_http_methods(request_method_list=["GET", "HEAD"])
def get_home(request: HttpRequest) -> HttpResponse: def get_home(request: HttpRequest) -> HttpResponse:
"""Render the index page with drops grouped hierarchically by game and campaign. """Render the index page with drops grouped hierarchically by game and campaign.