Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace exception rescue #24

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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