Add a base exception class and make unauthorization class a subclass.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Tue, 01 Feb 2011 04:32:53 +0530
changeset 541 a1007eb3fff9
parent 540 b07d52d49db7
child 542 23bf9b4611cb
Add a base exception class and make unauthorization class a subclass.
pytask/helpers/exceptions.py
pytask/middleware/exceptions.py
--- a/pytask/helpers/exceptions.py	Tue Feb 01 02:31:53 2011 +0530
+++ b/pytask/helpers/exceptions.py	Tue Feb 01 04:32:53 2011 +0530
@@ -10,11 +10,31 @@
 from django.utils.translation import ugettext
 
 
+DEFAULT_ERROR_MESSAGE = ugettext(
+  "There was some error in your request.")
+
 DEFAULT_LOGIN_MESSAGE = ugettext(
   "You have to login to view this page.")
 
 
-class UnauthorizedAccess(Exception):
+class PyTaskException(Exception):
+    """Base exception class to be used through out PyTask
+    """
+
+    def __init__(self, message=None, **response_args):
+        """Constructor specifying the exception specific attributes.
+        """
+
+        if not message:
+            message = DEFAULT_ERROR_MESSAGE
+
+        self.message = message
+        self.response_args = response_args
+
+        super(PyTaskException, self).__init__()
+
+
+class UnauthorizedAccess(PyTaskException):
     """Exception that is raised when some one tries to access a view
     without the right priviliges.
     """
@@ -26,8 +46,6 @@
         if not message:
             message = DEFAULT_LOGIN_MESSAGE
 
-        self.message = message
-        self.response_args = response_args
-        self.response_args['status'] = 401
+        response_args['status'] = 401
 
-        super(UnauthorizedAccess, self).__init__()
+        super(UnauthorizedAccess, self).__init__(message, **response_args)
--- a/pytask/middleware/exceptions.py	Tue Feb 01 02:31:53 2011 +0530
+++ b/pytask/middleware/exceptions.py	Tue Feb 01 04:32:53 2011 +0530
@@ -10,6 +10,7 @@
 from django.template import loader
 from django.template import RequestContext
 
+from pytask.helpers.exceptions import PyTaskException
 from pytask.helpers.exceptions import UnauthorizedAccess
 
 
@@ -21,7 +22,8 @@
         """Process the exception raised.
         """
 
-        if isinstance(exception, UnauthorizedAccess):
+        if (isinstance(exception, PyTaskException) or 
+          isinstance(exception, UnauthorizedAccess)):
             template = loader.get_template('error.html')
             context = RequestContext(request, {
               'error_message': exception.message