Skip to content

Commit

Permalink
Support rescue events in the exception tracer
Browse files Browse the repository at this point in the history
This feature is only activated in Ruby 3.3 and later. It allows the
tracer to print trace when exceptions are rescued.

Co-authored-by: Kaan Ozkan <[email protected]>
  • Loading branch information
st0012 and KaanOzkan committed Mar 22, 2024
1 parent 37aafc2 commit 376aeec
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ rescue StandardError
nil
end

#depth:1 #<RuntimeError: boom> at test.rb:4
#depth:0 #<RuntimeError: boom> raised at test.rb:4
#depth:1 #<RuntimeError: boom> rescued at test.rb:6
```

#### CallTracer
Expand Down
32 changes: 24 additions & 8 deletions lib/tracer/exception_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@

class ExceptionTracer < Tracer::Base
def setup_tp
TracePoint.new(:raise) do |tp|
next if skip?(tp)
if RUBY_VERSION >= "3.3.0"
TracePoint.new(:raise, :rescue) do |tp|
next if skip?(tp)

exc = tp.raised_exception
exc = tp.raised_exception

out tp,
" #{colorize_magenta(exc.inspect)}",
depth: caller.size - (1 + @depth_offset)
rescue Exception => e
p e
action = tp.event == :raise ? "raised" : "rescued"

out tp,
" #{colorize_magenta(exc.inspect)} #{action}",
depth: caller.size - (1 + @depth_offset)
rescue Exception => e
p e
end
else
TracePoint.new(:raise) do |tp|
next if skip?(tp)

exc = tp.raised_exception

out tp,
" #{colorize_magenta(exc.inspect)} raised",
depth: caller.size - (1 + @depth_offset)
rescue Exception => e
p e
end
end
end

Expand Down
32 changes: 26 additions & 6 deletions test/tracer/exception_tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,46 @@ def test_exception_tracer_traces_exceptions
file = write_file("foo.rb", <<~RUBY)
ExceptionTracer.new.start
raise "boom" rescue nil
begin
raise "boom"
rescue
end
RUBY

out, err = execute_file(file)

assert_empty(err)
assert_traces([/^#depth:0 #<RuntimeError: boom> at .*foo.rb:3/], out)

expected_traces = [
/^#depth:0 #<RuntimeError: boom> raised at .*foo.rb:4/
]

if RUBY_VERSION >= "3.3.0"
expected_traces << /^#depth:1 #<RuntimeError: boom> rescued at .*foo.rb:5/
end

assert_traces(expected_traces, out)
end

def test_exception_tracer_with_header
file = write_file("foo.rb", <<~RUBY)
ExceptionTracer.new(header: "tracer-1").start
raise "boom" rescue nil
begin
raise "boom"
rescue
end
RUBY

out, err = execute_file(file)
execute_file(file)

assert_empty(err)
assert_traces([/^tracer-1 #depth:0 #<RuntimeError: boom> at .*foo.rb:3/], out)
expected_traces = [
/^tracer-1 #depth:0 #<RuntimeError: boom> raised at .*foo.rb:4/
]

if RUBY_VERSION >= "3.3.0"
expected_traces << /^tracer-1 #depth:1 #<RuntimeError: boom> rescued at .*foo.rb:5/
end
end
end
end
10 changes: 9 additions & 1 deletion test/tracer/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ def test_trace_exception

out, err = execute_file(file)

expected_traces = [
/^#depth:1 #<RuntimeError: boom> raised at .*foo.rb:4/
]

if RUBY_VERSION >= "3.3.0"
expected_traces << /^#depth:2 #<RuntimeError: boom> rescued at .*foo.rb:4/
end

assert_empty(err)
assert_traces([/#depth:1 #<RuntimeError: boom> at .*foo.rb:4/], out)
assert_traces(expected_traces, out)
end

def test_trace_call
Expand Down

0 comments on commit 376aeec

Please sign in to comment.