From 70c9f9e24a31e02e8c171103db74b007d4b194a0 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 19 Oct 2018 00:45:04 +0200 Subject: [PATCH] use error views without templating, fixes #365 --- nsupdate/templates/400.html | 7 ----- nsupdate/templates/403.html | 9 ------ nsupdate/templates/404.html | 7 ----- nsupdate/templates/500.html | 7 ----- nsupdate/templates/errorpage.html | 14 --------- nsupdate/urls.py | 51 +++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 nsupdate/templates/400.html delete mode 100644 nsupdate/templates/403.html delete mode 100644 nsupdate/templates/404.html delete mode 100644 nsupdate/templates/500.html delete mode 100644 nsupdate/templates/errorpage.html diff --git a/nsupdate/templates/400.html b/nsupdate/templates/400.html deleted file mode 100644 index 0e6add6..0000000 --- a/nsupdate/templates/400.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "errorpage.html" %} - -{% block error %} -

400 Bad Request

- -This kind of request is not allowed here. -{% endblock %} diff --git a/nsupdate/templates/403.html b/nsupdate/templates/403.html deleted file mode 100644 index 2c5786e..0000000 --- a/nsupdate/templates/403.html +++ /dev/null @@ -1,9 +0,0 @@ -{% extends "errorpage.html" %} - -{% block error %} -

403 Forbidden

- -Sorry, this is not allowed (for you). - -If you did not log in yet, try logging in. -{% endblock %} diff --git a/nsupdate/templates/404.html b/nsupdate/templates/404.html deleted file mode 100644 index 0200324..0000000 --- a/nsupdate/templates/404.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "errorpage.html" %} - -{% block error %} -

404 Not found

- -Nothing to see here. -{% endblock %} diff --git a/nsupdate/templates/500.html b/nsupdate/templates/500.html deleted file mode 100644 index 3141f85..0000000 --- a/nsupdate/templates/500.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "errorpage.html" %} - -{% block error %} -

500 Internal Server Error

- -Sorry, we screwed up. Try again later. :( -{% endblock %} diff --git a/nsupdate/templates/errorpage.html b/nsupdate/templates/errorpage.html deleted file mode 100644 index 3a9a20f..0000000 --- a/nsupdate/templates/errorpage.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - {{ WWW_HOST }} - something went wrong... - - - - {% block error %} - {% endblock %} - - Back to {{ WWW_HOST }}. - - diff --git a/nsupdate/urls.py b/nsupdate/urls.py index deb88b1..bd559e3 100644 --- a/nsupdate/urls.py +++ b/nsupdate/urls.py @@ -2,11 +2,14 @@ top-level url dispatching """ +import six + from django.conf import settings from django.conf.urls import include, url from django.contrib import admin from django.contrib.auth import views as auth_views from django.conf.urls.static import static +from django.http import HttpResponse def remember_me_login(request, *args, **kw): @@ -35,3 +38,51 @@ if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) import debug_toolbar urlpatterns += [url(r'^__debug__/', include(debug_toolbar.urls)), ] + + +# we have expensive context processors and do not want to invoke them for the +# http error views, so we must not use templates (nor the django default views). + +def http_error(request, status, exception=None): + if exception is not None: + exception_repr = exception.__class__.__name__ + # Try to get an "interesting" exception message: + try: + message = exception.args[0] + except (AttributeError, IndexError): + pass + else: + if isinstance(message, six.text_type): + exception_repr = message + else: + # we do not have an exception for 500 + exception_repr = 'Server Error' + body = """\ +

%(exception)s (error %(status)d)

+""" % dict( + exception=exception_repr, + status=status, + ) + return HttpResponse(body, content_type='text/html', status=status) + + +def bad_request(request, exception, template_name=None): + return http_error(request, 400, exception) + + +def permission_denied(request, exception, template_name=None): + return http_error(request, 403, exception) + + +def page_not_found(request, exception, template_name=None): + return http_error(request, 404, exception) + + +def server_error(request, template_name=None): + return http_error(request, 500) + + +handler400 = 'nsupdate.urls.bad_request' +handler403 = 'nsupdate.urls.permission_denied' +handler404 = 'nsupdate.urls.page_not_found' +handler500 = 'nsupdate.urls.server_error'