Skip to content
Open
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
5 changes: 5 additions & 0 deletions sentry-rails/lib/sentry/rails/error_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions sentry-rails/spec/sentry/rails/error_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions sentry-rails/spec/sentry/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading