How to suppress backtrace in Rails?

When you exit a Rails application using raiseor fail, how do I prevent the backtrace from being displayed?

I tried to use it back_trace_limit, but it only works for the console ...?

+1
source share
1 answer

You have full control over the backtrace returned by the exception instance using its method set_backtrace. For instance:

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

Conclusion:

hello
Error message: ERROR!
Error backtrace: []

The strip_backtrace method detects all errors, sets the backtrace to an empty array, and repeatedly raises the thrown exception.

+1
source

All Articles