Thanks for submitting the patch, this is definitely a bug. 

There is one small change I need before I can merge this, though. If the debug flag is set then this block should reraise the error. For example, see the other exception handler in this function. 

It would also be good if you could add a test.