Implement custom exception handling so we can get errors properly
authorMagnus Hagander <magnus@hagander.net>
Mon, 24 Nov 2008 15:32:29 +0000 (15:32 +0000)
committerMagnus Hagander <magnus@hagander.net>
Mon, 24 Nov 2008 15:32:29 +0000 (15:32 +0000)
and not just throw a generic 500 Internal Server Error message.

planetadmin/exceptions.py [new file with mode: 0644]
planetadmin/register/templates/internal_error.html [new file with mode: 0644]
planetadmin/register/views.py
planetadmin/settings.py

diff --git a/planetadmin/exceptions.py b/planetadmin/exceptions.py
new file mode 100644 (file)
index 0000000..8f3f4b8
--- /dev/null
@@ -0,0 +1,12 @@
+from django.shortcuts import render_to_response
+
+class pExcept(Exception):
+        pass
+
+class PlanetExceptionMiddleware:
+       def process_exception(self, request, exception):
+               if isinstance(exception, pExcept):
+                       return render_to_response('internal_error.html', {
+                               'msg': exception
+                       })
+               return None
diff --git a/planetadmin/register/templates/internal_error.html b/planetadmin/register/templates/internal_error.html
new file mode 100644 (file)
index 0000000..6f24542
--- /dev/null
@@ -0,0 +1,5 @@
+{% extends "regbase.html" %}
+{%block regcontent%}
+<h1>Internal server error</h1>
+<p>{{msg}}</p>
+{%endblock%}
index 7a97de98cacc6c36d60d437d98a708f2ecbc82e9..dac19a02cf8f2b733387bb261c141728a230e5c5 100644 (file)
@@ -7,6 +7,7 @@ from django.core.mail import send_mail
 from django.db import transaction
 
 from planetadmin.register.models import *
+from planetadmin.exceptions import pExcept
 
 import socket
 import feedparser
@@ -14,9 +15,6 @@ import feedparser
 def issuperuser(user):
        return user.is_authenticated() and user.is_superuser
 
-class pExcept(Exception):
-       pass
-
 @login_required
 def root(request):
        if request.user.is_superuser:
@@ -31,10 +29,10 @@ def root(request):
 @transaction.commit_on_success
 def new(request):
        if not request.method== 'POST':
-               raise Exception('must be POST')
+               raise pExcept('must be POST')
        feedurl = request.POST['feedurl']
        if not len(feedurl) > 1:
-               raise Exception('must include blog url!')
+               raise pExcept('must include blog url!')
 
        # See if we can find the blog already
        try:
@@ -48,7 +46,7 @@ def new(request):
                # Found a match, so we're going to register this blog
                # For safety reasons, we're going to require approval before we do it as well :-P
                if not settings.NOTIFYADDR:
-                       raise Exception('Notify address not specified, cannot complete')
+                       raise pExcept('Notify address not specified, cannot complete')
                blog.userid = request.user.username
                blog.approved = False
                AuditEntry(request.user.username, 'Requested blog attachment for %s' % blog.feedurl).save()
@@ -80,7 +78,7 @@ So, head off to the admin interface and approve or reject this!
                return HttpResponse('Attempt to download blog feed returned status %s.' % (status))
        
        if not settings.NOTIFYADDR:
-               raise Exception('Notify address not specified, cannot complete')
+               raise pExcept('Notify address not specified, cannot complete')
 
        blog = Blog()
        blog.name = request.user.first_name
@@ -201,10 +199,7 @@ def __getvalidblogpost(request, blogid, postid):
        return post
 
 def __setposthide(request, blogid, postid, status):
-       try:
-               post = __getvalidblogpost(request, blogid, postid)
-       except pExcept, e:
-               return HttpResponse(e)
+       post = __getvalidblogpost(request, blogid, postid)
        post.hidden = status
        post.save()
        AuditEntry(request.user.username, 'Set post %s on blog %s visibility to %s' % (postid, blogid, status)).save()
@@ -223,10 +218,7 @@ def blogpost_unhide(request, blogid, postid):
 @login_required
 @transaction.commit_on_success
 def blogpost_delete(request, blogid, postid):
-       try:
-               post = __getvalidblogpost(request, blogid, postid)
-       except pExcept, e:
-               return HttpResponse(e)
+       post = __getvalidblogpost(request, blogid, postid)
 
        post.delete()
        AuditEntry(request.user.username, 'Deleted post %s from blog %s' % (postid, blogid)).save()
index 6b2300217bdc02b8244cbc6a0a969311139054c1..a5e77601cf4f822d4750133293a8aeef189954e4 100644 (file)
@@ -38,6 +38,7 @@ MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
+    'planetadmin.exceptions.PlanetExceptionMiddleware',
 )
 
 ROOT_URLCONF = 'planetadmin.urls'