1

When exiting a Rails app using raise or fail, how to prevent the backtrace from being displayed?

Tried using back_trace_limit but it only seems to work for the console...?

4

1 回答 1

1

您可以使用异常实例的set_backtrace方法完全控制返回的回溯。例如:

def strip_backtrace
  yield
rescue => err
  err.set_backtrace([])
  raise err
end

begin
  strip_backtrace do
    puts 'hello'
    raise 'ERROR!'
  end
rescue => err
  puts "Error message: #{err.message}"
  puts "Error backtrace: #{err.backtrace}"
end

输出:

hello
Error message: ERROR!
Error backtrace: []

此处的 strip_backtrace 方法捕获所有错误,将回溯设置为空数组,并重新引发修改后的异常。

于 2013-04-30T16:59:41.013 回答