Skip to content

Commit f5910f7

Browse files
authored
Merge pull request rails#51050 from Shopify/show-exception-report-error
ActionDispatch::Executor: report errors handled by ShowExceptions
2 parents 9940dc8 + 4067c95 commit f5910f7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

actionpack/lib/action_dispatch/middleware/executor.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def call(env)
1414
state = @executor.run!(reset: true)
1515
begin
1616
response = @app.call(env)
17+
if rendered_error = env["action_dispatch.exception"]
18+
@executor.error_reporter.report(rendered_error, handled: false, source: "application.action_dispatch")
19+
end
1720
returned = response << ::Rack::BodyProxy.new(response.pop) { state.complete! }
1821
rescue => error
1922
@executor.error_reporter.report(error, handled: false, source: "application.action_dispatch")

actionpack/lib/action_dispatch/middleware/show_exceptions.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ def call(env)
3434
request = ActionDispatch::Request.new env
3535
backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
3636
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception)
37+
request.set_header "action_dispatch.exception", wrapper.unwrapped_exception
38+
3739
if wrapper.show?(request)
38-
render_exception(request, wrapper)
40+
render_exception(request.dup, wrapper)
3941
else
4042
raise exception
4143
end
@@ -44,7 +46,6 @@ def call(env)
4446
private
4547
def render_exception(request, wrapper)
4648
status = wrapper.status_code
47-
request.set_header "action_dispatch.exception", wrapper.unwrapped_exception
4849
request.set_header "action_dispatch.original_path", request.path_info
4950
request.set_header "action_dispatch.original_request_method", request.raw_request_method
5051
fallback_to_html_format_if_invalid_mime_type(request)

actionpack/test/dispatch/executor_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ def test_body_abandoned
119119
assert_equal requests_count - 1, completed
120120
end
121121

122+
def test_error_reporting
123+
raised_error = nil
124+
error_report = assert_error_reported do
125+
raised_error = assert_raises TypeError do
126+
call_and_return_body { 1 + "1" }
127+
end
128+
end
129+
assert_same raised_error, error_report.error
130+
end
131+
132+
def test_error_reporting_with_show_exception
133+
middleware = Rack::Lint.new(
134+
ActionDispatch::Executor.new(
135+
ActionDispatch::ShowExceptions.new(
136+
Rack::Lint.new(->(_env) { 1 + "1" }),
137+
->(_env) { [500, {}, ["Oops"]] },
138+
),
139+
executor,
140+
)
141+
)
142+
143+
env = Rack::MockRequest.env_for("", {})
144+
error_report = assert_error_reported do
145+
middleware.call(env)
146+
end
147+
assert_instance_of TypeError, error_report.error
148+
end
149+
122150
private
123151
def call_and_return_body(&block)
124152
app = block || proc { [200, {}, []] }

0 commit comments

Comments
 (0)