优雅地中止rake任务

时间:2013-10-12 13:10:54

标签: ruby-on-rails rake

namespace:db do
    desc "Populates Database"
    task :populate => :environment do

    error = true

     if error
         puts "Error Code 1: There was an error!"
         return false
     end

end

正在运行

rake db:migrate

确实会导致预期的结果:

Error Code 1: There was an error!
rake aborted!
unexpected return

然后:

/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:131:in `block (3 levels) in <top (required)>'
/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:104:in `each'
/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:104:in `block (2 levels) in <top (required)>'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:fertilize7
(See full trace by running task with --trace)
james@ubuntu:~/Documents/fertilizer/fertilizer$ rake db:fertilize7
rake aborted!
/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:549: syntax error, unexpected end-of-input, expecting keyword_end
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:641:in `block in run_tasks_blocks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:641:in `each'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:641:in `run_tasks_blocks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/application.rb:244:in `run_tasks_blocks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:446:in `load_tasks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/james/Documents/fertilizer/fertilizer/Rakefile:6:in `<top (required)>'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
james@ubuntu:~/Documents/fertilizer/fertilizer$ 

如何摆脱所有错误报告,报告意外收益?它会分散真正的错误消息(“错误代码1:出现错误!”)。

1 个答案:

答案 0 :(得分:7)

你不是一个方法,你处在一个过程中。你不能回来,你应该next跳出proc。

error = true

 if error
     puts "Error Code 1: There was an error!"
     next
 end