3

Problem

I have something like this:

task :fail do
  exit 111
end

task :run_fail_and_succeed do
  begin
    Rake::Task['knapsack:fail'].invoke
  rescue
    exit 0
  end
end

I would like to run task :fail and handle it's exit status. Than exit with 0. How can I do that?

Notes:

  1. It can't be asynchronous / parallel / run in a background. :fail must finish before :run_fail_and_succeed.
  2. It would be nice not only to exit with 0, but really handle this status code, so I could do different things based on it's value
Community
  • 1
  • 1
ciembor
  • 7,189
  • 13
  • 59
  • 100

1 Answers1

5

rescue with a specific type of exception type. In your case, this should be SystemExit. Then you can check the fields associated with that exception.

task :run_fail_and_succeed do
  begin
    Rake::Task['knapsack:fail'].invoke
  rescue SystemExit => e
    puts e.status #=> 111
    # now you can handle your logic according to the exit status
  end
end
Arie Xiao
  • 13,909
  • 3
  • 31
  • 30
  • 3
    What if a task I run inside doesn't return exception? It execute some command inside, this command fail with error code, but my rescue block isn't called after all. – ciembor Apr 30 '15 at 14:06