diff --git a/sentry-ruby/lib/sentry/interfaces/exception.rb b/sentry-ruby/lib/sentry/interfaces/exception.rb index 79b4210f0..33ca12e9d 100644 --- a/sentry-ruby/lib/sentry/interfaces/exception.rb +++ b/sentry-ruby/lib/sentry/interfaces/exception.rb @@ -41,7 +41,7 @@ def self.build(exception:, stacktrace_builder:, mechanism:, data_collection:) data_collection: data_collection ) else - SingleExceptionInterface.new(exception: exception, mechanism: mechanism) + SingleExceptionInterface.new(exception: e, mechanism: mechanism) end end diff --git a/sentry-ruby/spec/sentry/client_spec.rb b/sentry-ruby/spec/sentry/client_spec.rb index 01a8e9ac8..81fe9c78e 100644 --- a/sentry-ruby/spec/sentry/client_spec.rb +++ b/sentry-ruby/spec/sentry/client_spec.rb @@ -485,6 +485,30 @@ module ExcTag; end end end + context 'when a cause shares its backtrace with its own cause' do + let(:exception) { build_exception_with_shared_backtrace_cause } + + it 'captures the type and message of each cause' do + expect(hash[:exception][:values].map { |value| value[:type] }).to eq(["ArgumentError", "TypeError", "RuntimeError"]) + expect(hash[:exception][:values][1][:value]).to match("exception b") + end + + it 'captures the shared stacktrace once' do + expect(hash[:exception][:values][0][:stacktrace]).not_to be_nil + expect(hash[:exception][:values][1][:stacktrace]).to be_nil + end + end + + context 'when a cause has no backtrace' do + let(:exception) { build_exception_with_stackless_cause } + + it 'captures the type and message of the cause' do + expect(hash[:exception][:values][0][:type]).to eq("Errno::ENOENT") + expect(hash[:exception][:values][0][:module]).to eq("Errno") + expect(hash[:exception][:values][0][:value]).to match("exception a") + end + end + context 'when the exception has a recursive cause' do let(:exception) { build_exception_with_recursive_cause } diff --git a/sentry-ruby/spec/spec_helper.rb b/sentry-ruby/spec/spec_helper.rb index c1863c7e5..20e055928 100644 --- a/sentry-ruby/spec/spec_helper.rb +++ b/sentry-ruby/spec/spec_helper.rb @@ -172,6 +172,32 @@ def build_exception_with_two_causes e end +def build_exception_with_shared_backtrace_cause + inner = begin + raise ArgumentError, "exception a" + rescue ArgumentError => e + e + end + + begin + begin + middle = TypeError.new("exception b") + middle.set_backtrace(inner.backtrace) + raise middle, cause: inner + rescue TypeError + raise "exception c" + end + rescue RuntimeError => e + e + end +end + +def build_exception_with_stackless_cause + raise "exception b", cause: Errno::ENOENT.new("exception a") +rescue RuntimeError => e + e +end + def build_exception_with_recursive_cause backtrace = []