Add a base exception class and make unauthorization class a subclass.
--- 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