views:

479

answers:

2

I have a block like so:

begin
      # some SQL request
rescue Mysql::Error => e
      logputs "Mysql::Error occurred, retrying in 10s: #{e.message}"
      sleep 10
      retry
end

But when a "Lost connection to MySQL server" error occurred, this block was not able to catch it and retry (the MySQL server was restarted). Any idea how I can properly catch this exception?

Thanks!

A: 

Are you sure you are rescuing the correct type of exception?

Try using the following code:

rescue StandardError => e
  puts e

That should output any exception that is raised and more importantly you will be able to see the specific type. Then you can adjust your rescue statement to be more specific.

Phil
Thanks. It looks I'm rescuing the correct type, since this is the error:/mnt/app/releases/20090620000135/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log': Mysql::Error: Lost connection to MySQL server during query: ... (ActiveRecord::StatementInvalid)
ambivalence
FYI -- Confusingly, the base class of most raised errors is not Exception but StandardError.
BaroqueBobcat
You're right BaroqueBobcat. I've adjusted the code.
Phil
+1  A: 

From your comment it looks like what's happening is that a Mysql::Error exception is being thrown, but then caught by ActiveRecord which then throws an ActiveRecord::StatementInvalid exception (which isn't very helpful behaviour in this case!).

I'd say change your rescue to catch the AR::StatementInvalid exception and see what that does for you.

Pete Hodgson