Skip to content

Commit

Permalink
feat: use after_change for context changes (#651)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
roelbondoc authored Nov 27, 2024
1 parent 6eeed8f commit 2cfc766
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/honeybadger/plugins/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 2cfc766

Please sign in to comment.