From 2c5215d3c9444ffa6f3f3656e994dcbcf5bb80d6 Mon Sep 17 00:00:00 2001 From: Gannon McGibbon Date: Thu, 20 Aug 2026 18:12:21 -0500 Subject: [PATCH] Make default_render failures not set an error cause A default_render that fails should not set a cause of a subsequent api_behavior call if it results in an exception in Responder#to_format. --- lib/action_controller/responder.rb | 14 +++++++------- test/action_controller/respond_with_test.rb | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/action_controller/responder.rb b/lib/action_controller/responder.rb index 6937a41..d1462b2 100644 --- a/lib/action_controller/responder.rb +++ b/lib/action_controller/responder.rb @@ -192,14 +192,14 @@ def to_js # responds to :to_format and display it. # def to_format - if !get? && has_errors? && !response_overridden? - display_errors - elsif has_view_rendering? || response_overridden? - default_render - else - api_behavior + begin + if !get? && has_errors? && !response_overridden? + return display_errors + elsif has_view_rendering? || response_overridden? + return default_render + end + rescue ActionView::MissingTemplate end - rescue ActionView::MissingTemplate api_behavior end diff --git a/test/action_controller/respond_with_test.rb b/test/action_controller/respond_with_test.rb index 655248c..9f4000e 100644 --- a/test/action_controller/respond_with_test.rb +++ b/test/action_controller/respond_with_test.rb @@ -732,6 +732,14 @@ def test_raises_missing_renderer_if_an_api_behavior_with_no_renderer end end + def test_api_behavior_error_is_not_caused_by_missing_template + @controller = CsvRespondWithController.new + error = assert_raise ActionController::MissingRenderer do + get :index, format: "csv" + end + assert_nil error.cause + end + def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called @controller = EmptyRespondWithController.new @request.accept = "*/*"