Skip to content

Commit 37fb99e

Browse files
authored
Merge pull request rails#41283 from tywhang/more_accurate_error_for_missing_file
Handle error when file does not exist at filepath
2 parents 831031a + 897809e commit 37fb99e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

actionview/lib/action_view/renderer/template_renderer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ def determine_template(options)
2626
if File.exist?(options[:file])
2727
Template::RawFile.new(options[:file])
2828
else
29-
raise ArgumentError, "`render file:` should be given the absolute path to a file. '#{options[:file]}' was given instead"
29+
if Pathname.new(options[:file]).absolute?
30+
raise ArgumentError, "File #{options[:file]} does not exist"
31+
else
32+
raise ArgumentError, "`render file:` should be given the absolute path to a file. '#{options[:file]}' was given instead"
33+
end
3034
end
3135
elsif options.key?(:inline)
3236
handler = Template.handler_for_extension(options[:type] || "erb")

actionview/test/template/render_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,20 @@ def test_render_file
7979

8080
def test_render_file_with_full_path_no_extension
8181
template_path = File.expand_path("../fixtures/test/hello_world", __dir__)
82-
assert_raise(ArgumentError) { @view.render(file: template_path) }
82+
e = assert_raise(ArgumentError) { @view.render(file: template_path) }
83+
assert_match(/File (.+) does not exist/, e.message)
84+
end
85+
86+
def test_render_file_with_invalid_full_path
87+
template_path = File.expand_path("../fixtures/test/hello_world_invalid.erb", __dir__)
88+
e = assert_raise(ArgumentError) { @view.render(file: template_path) }
89+
assert_match(/File (.+) does not exist/, e.message)
90+
end
91+
92+
def test_render_file_with_relative_path
93+
template_path = "fixtures/test/hello_world.erb"
94+
e = assert_raise(ArgumentError) { @view.render(file: template_path) }
95+
assert_match(%r{`render file:` should be given the absolute path to a file. (.+) was given instead}, e.message)
8396
end
8497

8598
# Test if :formats, :locale etc. options are passed correctly to the resolvers.

0 commit comments

Comments
 (0)