In my rails app, I am using ActiveResource to access another service, and I'd like to gracefully handle the exception that occurs when that resource is offline (server is down/ip is blocked etc.). With no response being returned, my app generates this error:
Errno::ECONNREFUSED in UsersController
I'd like to do a 'rescue_from' in my application controller so that it'll handle the error whenever it occurs, but not sure what the params would be, or it this kind of exception is even trappable at this point.
Should/can I test for the availability of a resource?
I can do this, but it catches every error, and I'd like to catch the specific connection type error I am getting.
rescue_from Exception, do
...
end
It might be easier to assume everything's okay, and then deal with the exception when it arises. You can use rescue_from in your application controller (or probably your users controller, if it's a local error):
class ApplicationController < ActionController::Base
rescue_from(Errno::ECONNREFUSED) do |e|
flash[:warning] = 'Hey! Bad things happened!'
render :template => 'my/error/page'
end
end
Note: I started this before you updated your post, but the good news is that Errno::ECONNREFUSED
is a class - not a constant - so you can use it instead of the generic Exception
:
irb(main):009:0> Errno::ECONNREFUSED
=> Errno::ECONNREFUSED
irb(main):010:0> Errno::ECONNREFUSED.class
=> Class
irb(main):011:0> Errno::ECONNREFUSED.superclass
=> SystemCallError
Hope that helps!