Skip to content

Commit

Permalink
Do not mask NoMethodError from within render_in
Browse files Browse the repository at this point in the history
Follow-up to rails#50665.

Unconditionally converting `NoMethodError` to `ArgumentError` can mask a
legitimate `NoMethodError` from within the `render_in` method.  This
commit adds a check to prevent that.
  • Loading branch information
jonathanhefner committed Jan 10, 2024
1 parent 75ba520 commit 952a13b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions actionview/lib/action_view/template/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def identifier

def render(context, *args)
@renderable.render_in(context)
rescue NoMethodError
raise ArgumentError, "'#{@renderable.inspect}' is not a renderable object. It must implement #render_in."
rescue NoMethodError => error
if error.name == :render_in
raise ArgumentError, "'#{@renderable.inspect}' is not a renderable object. It must implement #render_in."
else
raise
end
end

def format
Expand Down
9 changes: 9 additions & 0 deletions actionview/test/template/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ def test_render_renderable_with_incompatible_object
end
end

def test_render_renderable_does_not_mask_nomethoderror_from_within_render_in
renderable = Object.new
renderable.define_singleton_method(:render_in) { |*| nil.foo }

assert_raises NoMethodError, match: "undefined method `foo' for nil" do
@view.render renderable: renderable
end
end

def test_render_partial_starting_with_a_capital
assert_nothing_raised { @view.render(partial: "test/FooBar") }
end
Expand Down

0 comments on commit 952a13b

Please sign in to comment.