Skip to content

Commit

Permalink
[ruby/irb] Filter backtrace before format in handle_exception
Browse files Browse the repository at this point in the history
(ruby/irb#916)

handle_exception now applies the filter_backtrace to exception
backtraces prior to formatting the lines with Exception#full_message

This fixes a bug in upstream projects, notably Rails, where the
backtrace filtering logic expects the lines to be formatted as
Exception#backtrace.

ruby/irb@805ee008f9

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
  • Loading branch information
2 people authored and matzbot committed Apr 5, 2024
1 parent dbe8886 commit f87e60f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/irb.rb
Expand Up @@ -1222,6 +1222,13 @@ def handle_exception(exc)
irb_bug = true
else
irb_bug = false
# This is mostly to make IRB work nicely with Rails console's backtrace filtering, which patches WorkSpace#filter_backtrace
# In such use case, we want to filter the exception's backtrace before its displayed through Exception#full_message
# And we clone the exception object in order to avoid mutating the original exception
# TODO: introduce better API to expose exception backtrace externally
backtrace = exc.backtrace.map { |l| @context.workspace.filter_backtrace(l) }.compact
exc = exc.clone
exc.set_backtrace(backtrace)
end

if RUBY_VERSION < '3.0.0'
Expand All @@ -1246,7 +1253,6 @@ def handle_exception(exc)
lines = m.split("\n").reverse
end
unless irb_bug
lines = lines.map { |l| @context.workspace.filter_backtrace(l) }.compact
if lines.size > @context.back_trace_limit
omit = lines.size - @context.back_trace_limit
lines = lines[0..(@context.back_trace_limit - 1)]
Expand Down

0 comments on commit f87e60f

Please sign in to comment.