From 2cfc766660d0462168aded9c34712904f8a1f774 Mon Sep 17 00:00:00 2001 From: Roel Bondoc Date: Wed, 27 Nov 2024 11:06:54 -0500 Subject: [PATCH] feat: use after_change for context changes (#651) When an application uses `Rails.error.set_context` to set context, the gem does not see this data until an error is reported. This change uses the `ActiveSupport::ExecutionContext.after_change` callback so that we can always keep track of context changes. This ensures we can report errors with the proper context outside of Rails error report handling. --- lib/honeybadger/plugins/rails.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/honeybadger/plugins/rails.rb b/lib/honeybadger/plugins/rails.rb index 2954da1d..5b8b039d 100644 --- a/lib/honeybadger/plugins/rails.rb +++ b/lib/honeybadger/plugins/rails.rb @@ -32,8 +32,6 @@ def render_exception(arg, exception, *args) class ErrorSubscriber def self.report(exception, handled:, severity:, context: {}, source: nil) - Honeybadger.context(context) - # We only report handled errors (`Rails.error.handle`) # Unhandled errors will be caught by our integrations (eg middleware), # which have richer context than the Rails error reporter @@ -43,7 +41,7 @@ def self.report(exception, handled:, severity:, context: {}, source: nil) tags = ["severity:#{severity}", "handled:#{handled}"] tags << "source:#{source}" if source - Honeybadger.notify(exception, tags: tags) + Honeybadger.notify(exception, context: context, tags: tags) end def self.source_ignored?(source) @@ -67,6 +65,12 @@ def self.source_ignored?(source) end if Honeybadger.config[:'exceptions.enabled'] && defined?(::ActiveSupport::ErrorReporter) # Rails 7 + if defined?(::ActiveSupport::ExecutionContext) + ::ActiveSupport::ExecutionContext.after_change do + Honeybadger.context(::ActiveSupport::ExecutionContext.to_h) + end + end + ::Rails.error.subscribe(ErrorSubscriber) end end