QD-50 : merged in two patches/pull requests, made more changes, fixed the tests

https://bugs.indeed.com/browse/QD-50

  rebased onto latest from upstream, reapplied same patches, and my same fixes, and fixed the tests.
The assumption here is that this method will always be called from an except block.
If nothing else, this should be added as a comment. I'd also add a check, if possible. I'm not sure what happens when you call `raise` with no arguments when there is no exception in context.