Search code examples
rubyrake

Can I catch errors and continue to next Rake task?


I invoke Rake tasks in another Rake task like following:

namespace :test do
  task :migrate do
    Rake::Task['A:migrate'].invoke
    Rake::Task['B:migrate'].invoke
  end
end

What I want to know is whether I can continue to the next Rake task even if the earlier one fails. For instance, there is an error in A:migrate, I want to catch it and log it but move ahead with B:migrate. How can I do this?


Solution

  • Rake is just Ruby, so you can use Ruby's error handling feature.

    namespace :test do
      task :migrate do
        begin
          Rake::Task['A:migrate'].invoke
        rescue => e
          log(e)
        end
        Rake::Task['B:migrate'].invoke
      end
    end