Skip to content

Commit

Permalink
Filter backtrace before format in handle_exception
Browse files Browse the repository at this point in the history
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.

Co-authored-by: Hartley McGuire <[email protected]>
  • Loading branch information
joshbroughton and skipkayhil committed Apr 3, 2024
1 parent 2057248 commit 509aff6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1216,12 +1216,15 @@ def encode_with_invalid_byte_sequence(str, enc)
end

def handle_exception(exc)
exc = exc.dup
if exc.backtrace[0] =~ /\/irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ &&
!(SyntaxError === exc) && !(EncodingError === exc)
# The backtrace of invalid encoding hash (ex. {"\xAE": 1}) raises EncodingError without lineno.
irb_bug = true
else
irb_bug = false
backtrace = exc.backtrace.map { |l| @context.workspace.filter_backtrace(l) }.compact
exc.set_backtrace(backtrace)
end

if RUBY_VERSION < '3.0.0'
Expand All @@ -1246,7 +1249,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 509aff6

Please sign in to comment.