How to suppress backtrace in Rails?

拈花ヽ惹草 提交于 2020-01-16 01:30:11

问题


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...?


回答1:


You have total control over the backtrace returned with an exception instance by using its set_backtrace method. For example:

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

Output:

hello
Error message: ERROR!
Error backtrace: []

The strip_backtrace method here catches all errors, sets the backtrace to an empty array, and re-raises the modified exception.



来源:https://stackoverflow.com/questions/16304905/how-to-suppress-backtrace-in-rails

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