Skip to content

fix(rails): do not report the cause of an already captured view error twice - #3167

Open
pcbeingused33 wants to merge 5 commits into
getsentry:masterfrom
pcbeingused33:fix/capture-exception-cause-chain
Open

pcbeingused33 wants to merge 5 commits into
getsentry:masterfrom
pcbeingused33:fix/capture-exception-cause-chain

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3166
Resolves RUBY-239

Cause

With register_error_subscriber, an unhandled request exception is captured by Sentry::Rails::CaptureExceptions and also reported to Rails.error by ActionDispatch::Executor. For a view error the executor reports the unwrapped exception (the cause of ActionView::Template::Error), a different object, so ErrorSubscriber sent a second event without the wrapper's information.

Fix

ErrorSubscriber#report returns early for handled: false: unhandled exceptions are left to the CaptureExceptions middleware, which reports the outermost exception with the request data. Handled reports (Rails.error.handle, Rails.error.report(..., handled: true)) are unchanged. The earlier cause-marking change in CaptureExceptions is reverted.

Test

  • spec/sentry/rails/error_subscriber_spec.rb: a handled: false report is not captured.
  • spec/sentry/rails_spec.rb: with register_error_subscriber, Rails.error.report(..., handled: false) sends no event, and GET /view_exception sends exactly one event, an ActionView::Template::Error. All three fail without the change.

This pull request was prepared with the assistance of an AI coding agent (Claude Code) and submitted by Alex Castillo, who is responsible for its content.

…captured

Hub#capture_exception only skipped the exact exception object it had marked.
With register_error_subscriber, the middleware captures an
ActionView::Template::Error and the error subscriber later reports its cause,
which is a different object, so a view error became two events. An event
already carries the whole cause chain, so mark every exception in the chain
when capturing and skip any exception whose chain contains a marked one.

Fixes getsentry#3166

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Comment thread sentry-ruby/lib/sentry-ruby.rb Outdated
!!exc.instance_variable_get(CAPTURED_SIGNATURE)
# An event already carries the whole cause chain, so capturing any part of a
# chain that was captured would duplicate it.
Utils::ExceptionCauseChain.exception_to_array(exc).any? do |e|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes the capture semantics substantially, I'd rather prefer we fix this for the rails specific issue.

pcbeingused33 and others added 2 commits November 3, 2026 07:04
Narrow the fix to sentry-rails as requested in review: the previous commit
is reverted, so Sentry.exception_captured? and Hub#capture_exception keep
their original semantics.

Rails::CaptureExceptions now marks the causes of the exception it captured,
so ErrorSubscriber does not report them again, and it skips an exception
whose cause the error reporter already reported (development order).

Fixes getsentry#3166

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused33 pcbeingused33 changed the title fix(ruby): do not capture an exception whose cause chain was already captured fix(rails): do not report the cause of an already captured view error twice Nov 3, 2026
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Thanks, agreed. I reverted the core change (Hub#capture_exception and Sentry.exception_captured? are back to their original semantics) and moved the fix into Sentry::Rails::CaptureExceptions: it marks the causes of the exception it captured, so ErrorSubscriber doesn't report them again, and it skips an exception whose cause was already reported through the error reporter (the development order). Specs in sentry-rails/spec/sentry/rails/capture_exceptions_spec.rb cover both orders and an unrelated exception.

This comment was drafted with Claude Code.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a240ac1. Configure here.

CaptureExceptions marked the causes of the exception it handled even when
capture_exception returned no event (e.g. an excluded wrapper), so a later
ErrorSubscriber report of a non-excluded cause was dropped as a duplicate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Good catch, Bugbot was right: mark_causes_as_captured ran inside tap even when capture_exception returned no event, so an excluded wrapper (excluded_exceptions) marked its causes and a later ErrorSubscriber report of a non-excluded cause was dropped. In b0fe83b the causes are marked only when an event was actually sent, and a spec covers the excluded-wrapper case (it failed before the change).

This comment was drafted with Claude Code.

@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Hi @sl0thentr0py, friendly ping: the review feedback should be addressed now. Happy to adjust anything else. Thanks!

@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Hi, I believe the review feedback is addressed now. Could you take another look when you have a moment? Happy to adjust anything else. Thanks!

@sl0thentr0py

Copy link
Copy Markdown
Member

Sorry @pcbeingused33 I was on vacation.
Sadly, I still don't like this solution because it discards the exception with more info and we might throw away valuable information from the outer wrapper exception.

What we should do for now is actually not report unhandled exceptions at all from ErrorSubscriber since they are always handled by the CaptureExceptions middleware.

In the future, we will consider removing the CaptureExceptions middleware completely or using it only for older Rails versions.

Unhandled exceptions are already captured by the CaptureExceptions
middleware, which reports the outermost exception with the request data.
With register_error_subscriber enabled, Rails' executor also reports them
(for a view error, the cause of ActionView::Template::Error), producing a
second, less complete event.

ErrorSubscriber now skips reports with handled: false. This replaces the
previous CaptureExceptions cause-marking approach, which is reverted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused33
pcbeingused33 requested a review from a team as a code owner December 2, 2026 16:35
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

No worries, thanks for the review! Done in 30b910f: I reverted the CaptureExceptions change and ErrorSubscriber now skips reports with handled: false, so unhandled exceptions are only captured by the middleware with the outer exception and the request data. One side effect worth flagging: this also drops handled: false reports made outside a request (e.g. Rails.error.record in a runner script); happy to narrow it if you'd prefer. Specs cover the subscriber skip and a view error producing a single ActionView::Template::Error event, and I updated the PR description.

This comment was drafted with Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants