Fix stack trace middleware when multiple exceptions are thrown

Fixes issues #169 and #475. The only caveat is that the wrong stack trace might be printed if multiple exceptions with the same non-ref string message are thrown. But this corner case should be rare.