Question

I'm trying to run a command that might fail sometimes. When it fails, it throws an exception.

What I'd like it to do is just log the error quietly and continue executing the next line below it, rather than aborting and going into the 'rescue' block. How should I approach this?

My current code is as follows:

  rescue_from 'Gibbon::MailChimpError' do |exception|
    logger.error("MAILCHIMP: #{exception}")
  end

When I call the Mailchimp API, sometimes there is an error, and this disrupts the flow of my application. I just want it to carry on executing as if nothing has happened, and just note there was an error in the log.

Was it helpful?

Solution

How about something like this:

def rescuing(&block)
  begin
    yield
  rescue NameError => e
    puts "(Just rescued: #{e.inspect})"
  end
end

rescuing do
  puts "This is dangerous"
  raise NameError
end

puts "... but I'm still alive"

Obviously, you'd have to replace NameError with the exception you want to be protected against.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top