From 27d7384919b41c607f92ead49333bd307e49c9ca Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Mon, 28 Oct 2024 19:21:52 +0100 Subject: [PATCH] RescuedExceptionInterceptor: Handle empty configuration (#2428) Previously, it could happen that `Sentry.configuration` was `nil`. In this case, calling `rails` would produce a `NoMethodError`. We fix this issue by using safe navigation. Furthermore, this commit ensures we use a reasonable default in case the configuration couldn't be loaded. Since the config `report_rescued_exceptions` defaults to `true`, we assume this value here, too. Fixes #2386 --- CHANGELOG.md | 1 + .../lib/sentry/rails/rescued_exception_interceptor.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9dc18876..a11809b02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Fix Vernier profiler not stopping when already stopped [#2429](https://github.com/getsentry/sentry-ruby/pull/2429) - Fix `send_default_pii` handling in rails controller spans [#2443](https://github.com/getsentry/sentry-ruby/pull/2443) - Fixes [#2438](https://github.com/getsentry/sentry-ruby/issues/2438) +- Fix `RescuedExceptionInterceptor` to handle an empty configuration [#2428](https://github.com/getsentry/sentry-ruby/pull/2428) ## 5.21.0 diff --git a/sentry-rails/lib/sentry/rails/rescued_exception_interceptor.rb b/sentry-rails/lib/sentry/rails/rescued_exception_interceptor.rb index 43cb69db1..757377298 100644 --- a/sentry-rails/lib/sentry/rails/rescued_exception_interceptor.rb +++ b/sentry-rails/lib/sentry/rails/rescued_exception_interceptor.rb @@ -19,7 +19,16 @@ def call(env) end def report_rescued_exceptions? - Sentry.configuration.rails.report_rescued_exceptions + # In rare edge cases, `Sentry.configuration` might be `nil` here. + # Hence, we use a safe navigation and fallback to a reasonable default + # of `true` in case the configuration couldn't be loaded. + # See https://github.com/getsentry/sentry-ruby/issues/2386 + report_rescued_exceptions = Sentry.configuration&.rails&.report_rescued_exceptions + return report_rescued_exceptions unless report_rescued_exceptions.nil? + + # `true` is the default for `report_rescued_exceptions`, as specified in + # `sentry-rails/lib/sentry/rails/configuration.rb`. + true end end end