# HG changeset patch # User Madhusudan.C.S # Date 1296514973 -19800 # Node ID a1007eb3fff904ee2479bf72ba650d0ed5418d95 # Parent b07d52d49db776b14d3227bf75e00025011ad52e Add a base exception class and make unauthorization class a subclass. diff -r b07d52d49db7 -r a1007eb3fff9 pytask/helpers/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) diff -r b07d52d49db7 -r a1007eb3fff9 pytask/middleware/exceptions.py --- 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