Force ruby to hide backtrace on exception

好久不见. 提交于 2019-12-10 18:32:10

问题


I do some usual tasks with Rakefile, such as compilation, linking, etc.

When compilation fails ruby shows full backtrace in which task error happen, but it's really useless for me, even more: this backtrace hides compilation errors.

$ rake
mkdir -p build
llvm-as source/repl.ll -o build/repl.bc
llvm-as: source/repl.ll:6:22: error: expected value token
  call i32 @fputc(i8 'x', i32 0)
                 ^
rake aborted!
Command failed with status (1): [llvm-as source/repl.ll -o build/repl.bc...]
/usr/local/lib/ruby/1.9.1/rake.rb:993:in `block in sh'
/usr/local/lib/ruby/1.9.1/rake.rb:1008:in `call'
/usr/local/lib/ruby/1.9.1/rake.rb:1008:in `sh'
/usr/local/lib/ruby/1.9.1/rake.rb:1092:in `sh'
...

How to hide all after "rake aborted!" ?


回答1:


It seems that you are using the "sh" command in the rake task to start the compiler. In that case you should catch a RuntimeError, in order to get this error. Something like this:

task "foo" do
  begin
    sh "bar"
  rescue RuntimeError => e
    puts e.message
    exit(1)
  end
end


来源:https://stackoverflow.com/questions/5357383/force-ruby-to-hide-backtrace-on-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!