Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d459668

Browse files
committedMar 19, 2025··
Demo execution_context for error reporter
1 parent 5e660f7 commit d459668

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed
 

‎activesupport/lib/active_support/error_reporter.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def initialize(*subscribers, logger: nil)
3737
@logger = logger
3838
@debug_mode = false
3939
@context_middlewares = ErrorContextMiddlewareStack.new
40+
@execution_context = ExecutionContext.new
4041
end
4142

4243
# Evaluates the given block, reporting and swallowing any unhandled error.
@@ -200,7 +201,7 @@ def disable(subscriber)
200201
# Rails.error.set_context(section: "checkout", user_id: @user.id)
201202
#
202203
def set_context(...)
203-
ActiveSupport::ExecutionContext.set(...)
204+
@execution_context.set(...)
204205
end
205206

206207
# Add a middleware to modify the error context before it is sent to subscribers.
@@ -274,6 +275,10 @@ def report(error, handled: true, severity: handled ? :warning : :error, context:
274275
nil
275276
end
276277

278+
def clear_context
279+
@execution_context.clear
280+
end
281+
277282
private
278283
def ensure_backtrace(error)
279284
return if error.frozen? # re-raising won't add a backtrace

‎activesupport/lib/active_support/railtie.rb

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class Railtie < Rails::Railtie # :nodoc:
4444
app.executor.to_complete { ActiveSupport::ExecutionContext.clear }
4545
end
4646

47+
initializer "active_support.reset_error_reporter_context" do |app|
48+
app.reloader.before_class_unload { ActiveSupport.error_reporter.clear_context }
49+
app.executor.to_run { ActiveSupport.error_reporter.clear_context }
50+
app.executor.to_complete { ActiveSupport.error_reporter.clear_context }
51+
end
52+
4753
initializer "active_support.reset_all_current_attributes_instances" do |app|
4854
app.reloader.before_class_unload { ActiveSupport::CurrentAttributes.clear_all }
4955
app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }

0 commit comments

Comments
 (0)
Please sign in to comment.