Skip to content

Commit

Permalink
Merge pull request #24 from ruby/trace-exception-rescue
Browse files Browse the repository at this point in the history
Trace exception rescue
  • Loading branch information
st0012 authored Mar 22, 2024
2 parents 37aafc2 + 104946b commit 8ee01fd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 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
30 changes: 26 additions & 4 deletions test/tracer/exception_tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,48 @@ 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)

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_empty(err)
assert_traces([/^#depth:0 #<RuntimeError: boom> at .*foo.rb:3/], out)
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)

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

assert_empty(err)
assert_traces([/^tracer-1 #depth:0 #<RuntimeError: boom> at .*foo.rb:3/], out)
assert_traces(expected_traces, out)
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 8ee01fd

Please sign in to comment.