Django Unhandled Exception
It is running under DEBUG = True mode. Sometimes it can throw out an error message with traceback information when encounter an error but sometimes it just display the following lines:
Unhandled Exception
An unhandled exception was thrown by the application.
I have to switch to development server to see detail message.
How can I make it always display traceback message when an error is encountered?
Just connect to the got_request_exception signal and log the exception:
from django.core.signals import got_request_exception
import logging
def log(*args, **kwargs):
logging.exception('error')
got_request_exception.connect(log)
This will log the whole trace. In the dev server, it logs in the console.
Maybe you can use this snippet, this will log exceptions in apache's log:
utils.py
:
def log_traceback(exception, args):
import sys, traceback, logging
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
logging.debug(exception)
logging.debug(args)
for tb in traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback):
logging.debug(tb)
site_logging.py
:
import logging
import sys
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
Put it in your settings.py
:
import site_logging
And in your code:
from where.is.your.utils import log_traceback
try:
`do something`
except Exception, args:
log_traceback(Exception, args)
Are you using Apache?
Just out of interest is this your Production or Dev environment where you want to see the traceback?
From the DJango Book on security - Exposed error messages
Users deploying under Apache and mod_python should also make sure they have PythonDebug Off in their Apache conf files; this will ensure that any errors that occur before Django’s had a chance to load won’t be displayed publicly.
I assume you want PythonDebug On, this is recommended for Development only.
That's what DEBUG=True is for: to show the full traceback. The idea is that regular users do not want (nor do you want them to) see anything other than a simple error message.