1👍
✅
While writing an app, you can use the following approach:
Whenever your app raises an Exception, use custom, app-specific exceptions like this
class FooException(Exception):
"""Base class for all app specific exceptions"""
pass
classFooPermissionException(FooException):
"""Specific class for permission related exceptions"""
pass
Now your app can provide a custom middleware
class FooExceptionMiddleware(object):
"""App specific middleware to handle app specific errors"""
def process_exception(self, request, e):
"""Processes exceptions"""
if isinstance(e, FooException):
if isinstance(e, FooPermissionException):
# handle PermissionExceptions here...
return None
You can read more about Django middleware here.
Edit:
I think you can use the above approach, to catch all Exceptions
: Have a look at Django’s exception source code. Django’s internal exceptions are siblings of Exception
. So with something like the following code, you can possibly catch all exceptions, wherever they are raised in your project:
class AllExceptionMiddleware(object):
def process_exception(self, request, e):
if isinstance(e, Exception):
# do stuff
return None
2👍
Django has a an event logging functionality which uses Python logger to deliver all the tasks you’re looking for.
You can read more about it here
- [Django]-Django: searching on an EncryptedCharField (django-extensions), is this possible?
- [Django]-Django bulk remove and add many to many relationships
- [Django]-Django CONN_MAX_AGE setting error
Source:stackexchange.com