Forum: Ruby on Rails rescue_action_in_public question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kevin S. (Guest)
on 2006-02-16 20:42
(Received via mailing list)
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 S.
Steve R. (Guest)
on 2006-02-16 21:08
Look at the wiki page:

http://wiki.rubyonrails.org/rails/pages/HowtoConfi...

There's a good discussion about where errors can occur and what to do
about them.

Steve

Kevin S. 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 S.
Steve R. (Guest)
on 2006-02-17 05:59
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 S. 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 S.
This topic is locked and can not be replied to.