I am running into a situation where I can’t ensure the proper sequence of Rack middlewares. I have two middlewares in my Rails 3.0.9 stack:
The first one is a gem that notifies me (via email) of any unhandled exception thrown by my app. The second one is to handle the display of error pages. The call methods of these two are straightforward. The first one calls its predecessor and catches resulting exception, sends email and re-raises the same exception. The second one just enhances already available functionality in Rails to render properly formatted pages. The second one does not raise any exception and thus renders the response and everything is all right.
In an initializer, I have a code (link to gist) that inserts the 1st one before the 2nd one.
But in reality, ActionDispatch::ShowException is always called first and when I get an exceptional condition, the properly formatted error pages are displayed, but email is never sent because the ExceptionNotifier is never called.
Does that make sense? Anybody has any comment about how I can make it do the right thing? (Call ExceptionNotifier first and ShowExceptions later).
PS: output of rake middleware with this setup: