From 2d7c9b181f8106e0f1b3480a141e6b715025887b Mon Sep 17 00:00:00 2001 From: Rodrigo Serradura Date: Fri, 15 Mar 2024 11:09:22 -0300 Subject: [PATCH] Improve backtrace cleaner (examples) --- .../lib/transitions_listener/stdout.rb | 4 +++- .../single_listener/lib/single_transitions_listener.rb | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/multiple_listeners/lib/transitions_listener/stdout.rb b/examples/multiple_listeners/lib/transitions_listener/stdout.rb index dc2134a..9f8fd46 100644 --- a/examples/multiple_listeners/lib/transitions_listener/stdout.rb +++ b/examples/multiple_listeners/lib/transitions_listener/stdout.rb @@ -49,9 +49,11 @@ def before_interruption(exception:, transitions:) dir = "#{FileUtils.pwd[1..]}/" + listener_filename = File.basename(__FILE__).chomp('.rb') + cb = bc.clean(exception.backtrace) cb.each { _1.sub!(dir, '') } - cb.reject! { _1.match?(/block \(\d levels?\) in|in `block in|internal:kernel/) } + cb.reject! { _1.match?(/block \(\d levels?\) in|in `block in|internal:kernel|#{listener_filename}/) } puts "\nException:\n #{exception.message} (#{exception.class})\n\nBacktrace:\n #{cb.join("\n ")}" end diff --git a/examples/single_listener/lib/single_transitions_listener.rb b/examples/single_listener/lib/single_transitions_listener.rb index 2d25f36..ae3b9cf 100644 --- a/examples/single_listener/lib/single_transitions_listener.rb +++ b/examples/single_listener/lib/single_transitions_listener.rb @@ -104,8 +104,14 @@ def before_interruption(exception:, transitions:) bc.add_silencer { |line| /lib\/bcdd\/result/.match?(line) } bc.add_silencer { |line| line.include?(RUBY_VERSION) } - backtrace = bc.clean(exception.backtrace) + dir = "#{FileUtils.pwd[1..]}/" - puts "\nException: #{exception.message} (#{exception.class}); Backtrace: #{backtrace.join(", ")}" + listener_filename = File.basename(__FILE__).chomp('.rb') + + cb = bc.clean(exception.backtrace) + cb.each { _1.sub!(dir, '') } + cb.reject! { _1.match?(/block \(\d levels?\) in|in `block in|internal:kernel|#{listener_filename}/) } + + puts "\nException:\n #{exception.message} (#{exception.class})\n\nBacktrace:\n #{cb.join("\n ")}" end end