diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 72cc9090bcd5e97cf49696055450c6fa382b6774..159aae3993363b4cc7eb36375dfddd30bc0914d7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,11 @@ Added * Allow to configure port for prometheus metrics endpoint. +Fixed +~~~~~ + +* Correctly deliver server errors to user + `2.0rc3`_ - 2021-07-26 ---------------------- diff --git a/aleksis/core/urls.py b/aleksis/core/urls.py index 63de04ac714a9a7236ae26bfb95d9d417b081a32..b09c6ff9456e1b7e749301a4f261fb5e642e380c 100644 --- a/aleksis/core/urls.py +++ b/aleksis/core/urls.py @@ -226,6 +226,9 @@ urlpatterns = [ path("pdfs/<int:pk>/", views.RedirectToPDFFile.as_view(), name="redirect_to_pdf_file"), ] +# Use custom server error handler to get a request object in the template +handler500 = views.server_error + # Add URLs for optional features if hasattr(settings, "TWILIO_ACCOUNT_SID"): from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls # noqa diff --git a/aleksis/core/views.py b/aleksis/core/views.py index 416417406cdc6258335ec70553d7ac2259f2dbec..343bd9149d9ae469973e9a32fb85c91e92875d0c 100644 --- a/aleksis/core/views.py +++ b/aleksis/core/views.py @@ -13,15 +13,18 @@ from django.http import ( HttpResponse, HttpResponseNotFound, HttpResponseRedirect, + HttpResponseServerError, JsonResponse, ) from django.http.response import FileResponse from django.shortcuts import get_object_or_404, redirect, render +from django.template import loader from django.urls import reverse, reverse_lazy from django.utils.decorators import method_decorator from django.utils.translation import get_language from django.utils.translation import gettext_lazy as _ from django.views.decorators.cache import never_cache +from django.views.defaults import ERROR_500_TEMPLATE_NAME from django.views.generic.base import TemplateView, View from django.views.generic.detail import DetailView, SingleObjectMixin from django.views.generic.edit import DeleteView, UpdateView @@ -1198,3 +1201,13 @@ class SocialAccountDeleteView(DeleteView): self.request, _("The third-party account has been successfully disconnected.") ) return HttpResponseRedirect(success_url) + + +def server_error( + request: HttpRequest, template_name: str = ERROR_500_TEMPLATE_NAME +) -> HttpResponseServerError: + """Custom error view that ensures the request is passed to the error page.""" + template = loader.get_template(template_name) + context = {"request": request} + + return HttpResponseServerError(template.render(context))