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...?
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...?
您可以使用异常实例的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 方法捕获所有错误,将回溯设置为空数组,并重新引发修改后的异常。