0
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:出现错误!”)。

4

1 回答 1

7

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

error = true

 if error
     puts "Error Code 1: There was an error!"
     next
 end
于 2013-10-12T13:12:40.897 回答