From f9852851f2b1060af089a1a0b40bb1c8628f6cbb Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sun, 1 Aug 2021 17:26:46 +0200
Subject: [PATCH] Make 5xx errors work again

---
 CHANGELOG.rst         |  5 +++++
 aleksis/core/urls.py  |  3 +++
 aleksis/core/views.py | 13 +++++++++++++
 3 files changed, 21 insertions(+)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 72cc9090b..159aae399 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 63de04ac7..b09c6ff94 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 416417406..343bd9149 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))
-- 
GitLab