With `register_error_subscriber`, view errors are reported twice: the wrapper by the middleware, and its cause by the error subscriber

Offen
#3,166 1 Kommentar 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

@sl0thentr0py arbeitet bereits daran.

Seit 03.11.2026.

  • #3167 von @pcbeingused333 — offen

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
57/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
rails, ruby

Rechercherichtung

Start in Hub#capture_exception and trace how captured markers and Sentry::Utils::ExceptionCauseChain handle exceptions and their causes. The report describes a reproduction using Rails 8.1.4 and sentry-rails 7.1.0; run it with reloading both off and on, then add focused coverage for each order. Done means one event per view error, retaining request context.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Bug Ruby
Issue Description

With config.rails.register_error_subscriber = true, every exception raised in a view becomes two Sentry issues.

  • Sentry::Rails::CaptureExceptions captures the ActionView::Template::Error and marks that object with @__sentry_captured.
  • ActionDispatch::Executor reports ExceptionWrapper#unwrapped_exception to Rails.error. For a template error, that's the wrapper's cause.
  • Sentry::Rails::ErrorSubscriber forwards that report. The cause isn't the marked object, so Hub#capture_exception doesn't skip it.

The two events have different types and stacks, so they group separately. When reloading is off, as in production, the executor reports outside the middleware's scope, so the second event also has no request URL or transaction. In development, ActionDispatch::Reloader reports the cause first, from inside the scope.

Related, from the other direction: #2931

Reproduction Steps

Below is a single-file app. enable_reloading = false matches production, and setting it to true shows the development order.

require "bundler/inline"
gemfile { source "https://rubygems.org"; gem "rails", "8.1.4"; gem "sentry-rails", "7.1.0" }
require "action_controller/railtie"

EVENTS = []

class ReproApp < Rails::Application
  config.root = __dir__
  config.eager_load = false
  config.enable_reloading = false
  config.consider_all_requests_local = false
  config.hosts.clear
  config.secret_key_base = "x" * 64
  config.logger = Logger.new(nil)
  routes.append { get "/" => "pages#show" }
end

Sentry.init do |c|
  c.dsn = "https://[email protected]/0"
  c.transport.transport_class = Sentry::DummyTransport
  c.background_worker_threads = 0
  c.rails.register_error_subscriber = true
  c.before_send = ->(event, hint) { EVENTS << "#{hint[:exception].class} url=#{event.request&.url.inspect}"; nil }
end

class PagesController < ActionController::Base
  def show = render(inline: "<%= raise 'boom' %>")
end

ReproApp.initialize!
ReproApp.call(Rack::MockRequest.env_for("/"))
p EVENTS

Output:

["ActionView::Template::Error url=\"http://example.org/\"", "RuntimeError url=nil"]
Expected Behavior

One event per view error, with request context.

Suggested fix

We tested this in the same app: it produced one event with request context, with reloading on and off. In Hub#capture_exception:

  • skip capturing when any exception in the cause chain is already marked as captured;
  • mark the whole chain after capturing.

The event already includes the chain, via Sentry::Utils::ExceptionCauseChain, so a later capture of any part of it duplicates information Sentry already has.

Versions

Ruby 4.0.9, Rails 8.1.4, sentry-ruby / sentry-rails 7.1.0.

Workaround

Leave register_error_subscriber off and subscribe a Rails.error subscriber that forwards only handled: true reports.

Vorherrschende Sprache
Ruby
Sterne
990
Forks
552
Ø Merge
2 T. 16 Std.
Gemergte PRs (30 T.)
29

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus getsentry/sentry-ruby

Alle Issues in getsentry/sentry-ruby

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.