Occasionally we are getting OperationalError: FATAL and we have no idea why. I want to handle this error wherever it happens in the application and send me a personal email. I would also like to set up a system command call to inspect the database activity (I know this is a bad idea but it's the only thing I can think of to try to figure out why this is happening).
How can I do this? Summarized: catch an error of a specific type raised at any point and handle it in a custom and granular way.
You can create an middleware to handle your exception. See https://docs.djangoproject.com/en/2.2/topics/http/middleware/#process-exception
For example