diff --git a/sentry-rails/lib/sentry/rails/error_subscriber.rb b/sentry-rails/lib/sentry/rails/error_subscriber.rb index 39ba5be42..dc6c7a3e5 100644 --- a/sentry-rails/lib/sentry/rails/error_subscriber.rb +++ b/sentry-rails/lib/sentry/rails/error_subscriber.rb @@ -9,6 +9,11 @@ class ErrorSubscriber SKIP_SOURCES = Regexp.union([/.*_cache_store.active_support/]) def report(error, handled:, severity:, context:, source: nil) + # Unhandled exceptions are already captured by the CaptureExceptions middleware, + # which reports the outermost exception with the request data. Reporting them here + # too would send a second event, e.g. for the cause of an ActionView::Template::Error. + return unless handled + tags = { handled: handled } if source diff --git a/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb index 6b2e36d80..f047b47a7 100644 --- a/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/error_subscriber_spec.rb @@ -81,10 +81,8 @@ end context 'when handled: false' do - it 'sets mechanism.handled to false' do - expect(Sentry::Rails).to receive(:capture_exception) do |_, hint:, **_| - expect(hint[:mechanism].handled).to eq(false) - end + it 'does not capture the exception' do + expect(Sentry::Rails).not_to receive(:capture_exception) described_class.new.report(StandardError.new, handled: false, severity: :error, context: {}) end end diff --git a/sentry-rails/spec/sentry/rails_spec.rb b/sentry-rails/spec/sentry/rails_spec.rb index b93435cdd..5f8fa1a3c 100644 --- a/sentry-rails/spec/sentry/rails_spec.rb +++ b/sentry-rails/spec/sentry/rails_spec.rb @@ -331,15 +331,30 @@ end it "registers Sentry::Rails::ErrorSubscriber to Rails" do - Rails.error.report(Exception.new, handled: false) + Rails.error.report(Exception.new, handled: true) expect(transport.events.count).to eq(1) - ActiveSupport.error_reporter.report(Exception.new, handled: false) + ActiveSupport.error_reporter.report(Exception.new, handled: true) expect(transport.events.count).to eq(2) end + it "doesn't report unhandled exceptions, which CaptureExceptions already captures" do + Rails.error.report(Exception.new, handled: false) + + expect(transport.events.count).to eq(0) + end + + it "reports a view error only once" do + get "/view_exception" + + expect(transport.events.count).to eq(1) + + event = transport.events.first + expect(event.exception.values.last.type).to eq("ActionView::Template::Error") + end + it "sets correct contextual data to the reported event" do Rails.error.handle(severity: :info, context: { foo: "bar" }) do 1/0