Improvements to the middleware's exception handling.