I''m using the rescue_action_in_public example from the Agile book to handle errors and email serious ones to me. In my application.rb controller: def rescue_action_in_public(exception) logger.error("rescue_action_in_public executed") case exception when ActiveRecord::RecordNotFound, ActionController::RoutingError, ActionController::UnknownAction logger.error("404 displayed") render(:file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found") else logger.error("500 displayed") render(:file => "#{RAILS_ROOT}/public/500.html", :status => "500 Error") SystemNotifier.deliver_exception_notification(self, request, exception) end end It works great when the exception thrown is ActiveRecord::RecordNotFound. It calls rescue_action_in_public and logs everything as expected. If I ask it to send me an email about it, it does that too. But it is not working for ActionController::RoutingError. It still logs the error and displays the 404 page to the user, but doesn''t seem to use rescue_action_in_public in the process. My custom log messages don''t get added and if I ask it to send me an email it doesn''t do that either. Any ideas on why the RoutingError exception wouldn''t use my custom rescue_action_in_public while RecordNotFound would? Also, on a related note, does setting the .htaccess file in public to specify 404 and 500 pages affect rescue_action_in_public? (I don''t have them specified currently.) Does one of them override the other or is it good practice to use both? Thanks, Kevin Skoglund
Look at the wiki page: http://wiki.rubyonrails.org/rails/pages/HowtoConfigureTheErrorPageForYourRailsApp There''s a good discussion about where errors can occur and what to do about them. Steve Kevin Skoglund wrote:> I''m using the rescue_action_in_public example from the Agile book to > handle errors and email serious ones to me. In my application.rb > controller: > > def rescue_action_in_public(exception) > logger.error("rescue_action_in_public executed") > case exception > when ActiveRecord::RecordNotFound, ActionController::RoutingError, > ActionController::UnknownAction > logger.error("404 displayed") > render(:file => "#{RAILS_ROOT}/public/404.html", > :status => "404 Not Found") > else > logger.error("500 displayed") > render(:file => "#{RAILS_ROOT}/public/500.html", > :status => "500 Error") > SystemNotifier.deliver_exception_notification(self, request, > exception) > end > end > > It works great when the exception thrown is > ActiveRecord::RecordNotFound. It calls rescue_action_in_public and > logs everything as expected. If I ask it to send me an email about > it, it does that too. > > But it is not working for ActionController::RoutingError. It still > logs the error and displays the 404 page to the user, but doesn''t > seem to use rescue_action_in_public in the process. My custom log > messages don''t get added and if I ask it to send me an email it > doesn''t do that either. > > Any ideas on why the RoutingError exception wouldn''t use my custom > rescue_action_in_public while RecordNotFound would? > > Also, on a related note, does setting the .htaccess file in public to > specify 404 and 500 pages affect rescue_action_in_public? (I don''t > have them specified currently.) Does one of them override the other > or is it good practice to use both? > > Thanks, > Kevin Skoglund-- Posted via http://www.ruby-forum.com/.
Thanks, Steve. That wiki page implies that, since the Agile book was written, Rails has changed so that ActionController::RoutingError can''t be caught by patching rescue_action_in_public in application.rb. Instead you have to catch the exception earlier. My guess is this problem affects the exception_notification plug-in too. Can anyone confirm that? I did some more digging and discovered this ticket (http:// dev.rubyonrails.org/ticket/2536). I updated the version on the ticket to 1.0.0. I''ll live with the problem for now and wait for the fix. Kevin On Feb 16, 2006, at 2:08 PM, rails-request@lists.rubyonrails.org wrote:> Look at the wiki page: > > http://wiki.rubyonrails.org/rails/pages/ > HowtoConfigureTheErrorPageForYourRailsApp > > There''s a good discussion about where errors can occur and what to do > about them. > > Steve
Like every framework, there''s an order in which a Rails application handles a request. Until Rails has effectively mapped the route, it will not have the controller, model, etc. Exceptions that occur in setup code such as serving the page (5xx) and resolving the page (4xx) can''t be dealt with in a given controller or even in the base for all your controllers. Some frameworks (.NET comes to mind) are a tree structure where everything is derived from a single base class. That makes it possible to trap exceptions at the last possible moment and do some clever page not found rerouting. Rails seems to me more like a (small) bunch of bushes than a tree. Because of this decoupling, Rails can be very dynamic and self-discovering, but it does not seem like there''s a straightforward way to handle the bail-out case for exceptions. I''d sure like to hear how others are handling this. Kevin Skoglund wrote:> I''m using the rescue_action_in_public example from the Agile book to > handle errors and email serious ones to me. In my application.rb > controller: > > def rescue_action_in_public(exception) > logger.error("rescue_action_in_public executed") > case exception > when ActiveRecord::RecordNotFound, ActionController::RoutingError, > ActionController::UnknownAction > logger.error("404 displayed") > render(:file => "#{RAILS_ROOT}/public/404.html", > :status => "404 Not Found") > else > logger.error("500 displayed") > render(:file => "#{RAILS_ROOT}/public/500.html", > :status => "500 Error") > SystemNotifier.deliver_exception_notification(self, request, > exception) > end > end > > It works great when the exception thrown is > ActiveRecord::RecordNotFound. It calls rescue_action_in_public and > logs everything as expected. If I ask it to send me an email about > it, it does that too. > > But it is not working for ActionController::RoutingError. It still > logs the error and displays the 404 page to the user, but doesn''t > seem to use rescue_action_in_public in the process. My custom log > messages don''t get added and if I ask it to send me an email it > doesn''t do that either. > > Any ideas on why the RoutingError exception wouldn''t use my custom > rescue_action_in_public while RecordNotFound would? > > Also, on a related note, does setting the .htaccess file in public to > specify 404 and 500 pages affect rescue_action_in_public? (I don''t > have them specified currently.) Does one of them override the other > or is it good practice to use both? > > Thanks, > Kevin Skoglund-- Posted via http://www.ruby-forum.com/.
Possibly Parallel Threads
- Handling Erros from AWDWR
- AWDWR: NameError (uninitialized constant UnknownAction) in rescue_action_in_public
- routing error does not get caught by rescue_action_in_public
- issue with rescue_action_in_public! and testing 404 responses
- Missing action error not being caught