0

I am looking for some way in django's development server that will make the server to stop at any uncaught exception automatically, as it is done with pdb mode in ipython console.

I know to put import pdb; pdb.set_trace() lines into the code to make application stop. But this doesn't help me, because the line where the exception is thrown is being called too many times. So I can't find out the exact conditions to define a conditional break point.

Is this possible?

Thank you...

Mert Nuhoglu
  • 9,695
  • 16
  • 79
  • 117

1 Answers1

2

You can set sys.excepthook to a function that does import pdb; pdb.pm(), as per this recipe.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Somehow I can't override sys.excepthook: http://stackoverflow.com/questions/1261668/cannot-override-sys-excepthook Is this normal? – Mert Nuhoglu Aug 11 '09 at 16:59
  • @Mert, as I explained answering that question, ipython (which in that question you mention you're using) is different. So either run `ipython -pdb`, or plain `python` which this excepthook override. – Alex Martelli Aug 12 '09 at 01:02
  • @Alex, Thanks for the explanation. I have one more question: I can catch any uncaught exception through overriding sys.excepthook when I run a python script. That's good. But this recipe doesn't work when I run django server. The exception is caught and logged by django. Is it possible to make pdb catch the exception? – Mert Nuhoglu Aug 12 '09 at 08:55
  • Ok, I got it. Django's debug module catches and logs any uncaught exception. pdb doesn't catch the exception because the recipe makes pdb to catch any uncaught exception, not caught exceptions. – Mert Nuhoglu Aug 12 '09 at 09:20
  • @Mert, yep. There are actually ways to hook up a debugger, esp. if you're running django on WSGI, see http://code.google.com/p/modwsgi/wiki/DebuggingTechniques -- but setting sys.excepthook is still all about uncaught exceptions. – Alex Martelli Aug 12 '09 at 15:31
  • I have just found Werkzeug tool. That is very useful for debugging where an exception occurs. http://ericholscher.com/blog/2008/aug/28/screencast-debugging-django-error-page/ – Mert Nuhoglu Aug 28 '09 at 12:32