Skip to content

Commit 2cfc766

Browse files
authored
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.
1 parent 6eeed8f commit 2cfc766

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/honeybadger/plugins/rails.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ def render_exception(arg, exception, *args)
3232

3333
class ErrorSubscriber
3434
def self.report(exception, handled:, severity:, context: {}, source: nil)
35-
Honeybadger.context(context)
36-
3735
# We only report handled errors (`Rails.error.handle`)
3836
# Unhandled errors will be caught by our integrations (eg middleware),
3937
# which have richer context than the Rails error reporter
@@ -43,7 +41,7 @@ def self.report(exception, handled:, severity:, context: {}, source: nil)
4341

4442
tags = ["severity:#{severity}", "handled:#{handled}"]
4543
tags << "source:#{source}" if source
46-
Honeybadger.notify(exception, tags: tags)
44+
Honeybadger.notify(exception, context: context, tags: tags)
4745
end
4846

4947
def self.source_ignored?(source)
@@ -67,6 +65,12 @@ def self.source_ignored?(source)
6765
end
6866

6967
if Honeybadger.config[:'exceptions.enabled'] && defined?(::ActiveSupport::ErrorReporter) # Rails 7
68+
if defined?(::ActiveSupport::ExecutionContext)
69+
::ActiveSupport::ExecutionContext.after_change do
70+
Honeybadger.context(::ActiveSupport::ExecutionContext.to_h)
71+
end
72+
end
73+
7074
::Rails.error.subscribe(ErrorSubscriber)
7175
end
7276
end

0 commit comments

Comments
 (0)