From 4c85c0e6949e2747d5d03106e43ad0e6a8e612cf Mon Sep 17 00:00:00 2001
From: Őry Máté <ory.mate@cloud.bme.hu>
Date: Fri, 18 Jul 2014 23:15:18 +0200
Subject: [PATCH] dashboard: show unhandled HumanReadableExceptions on http500 page

---
 circle/circle/urls.py     |  2 ++
 circle/common/views.py    | 33 +++++++++++++++++++++++++++++++++
 circle/templates/500.html |  8 +++++++-
 3 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 circle/common/views.py

diff --git a/circle/circle/urls.py b/circle/circle/urls.py
index ceb623e..f5c51b9 100644
--- a/circle/circle/urls.py
+++ b/circle/circle/urls.py
@@ -64,3 +64,5 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE':
         '',
         (r'^saml2/', include('djangosaml2.urls')),
     )
+
+handler500 = 'common.views.handler500'
diff --git a/circle/common/views.py b/circle/common/views.py
new file mode 100644
index 0000000..4dc21a9
--- /dev/null
+++ b/circle/common/views.py
@@ -0,0 +1,33 @@
+from sys import exc_info
+
+import logging
+
+from django.template import RequestContext
+from django.shortcuts import render_to_response
+
+from .models import HumanReadableException
+
+logger = logging.getLogger(__name__)
+
+
+def handler500(request):
+    cls, exception, traceback = exc_info()
+    logger.exception("unhandled exception")
+    ctx = {}
+    if isinstance(exception, HumanReadableException):
+        try:
+            ctx['error'] = exception.get_user_text()
+        except:
+            pass
+        else:
+            try:
+                if request.user.is_superuser():
+                    ctx['error'] = exception.get_admin_text()
+            except:
+                pass
+        try:
+            resp = render_to_response("500.html", ctx, RequestContext(request))
+        except:
+            resp = render_to_response("500.html", ctx)
+        resp.status_code = 500
+        return resp
diff --git a/circle/templates/500.html b/circle/templates/500.html
index 93a17c1..8230fb3 100644
--- a/circle/templates/500.html
+++ b/circle/templates/500.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "dashboard/base.html" %}
 {% load i18n %}
 
 {% block title %}HTTP 500{% endblock %}
@@ -6,5 +6,11 @@
 {% block page_title %}{% trans ":(" %}{% endblock page_title %}
 
 {% block content %}
+<div style="margin-top: 4em;">
+{% if error %}
+<p>{{ error }}</p>
+{% else %}
 <p>{% trans "Internal Server Error... Please leave the server alone..." %}</p>
+{% endif %}
+</div>
 {% endblock content %}
--
libgit2 0.26.0