Display more specific error when snippet cannot be found

This commit is contained in:
Julia Boutin 2025-10-29 07:33:46 -06:00
parent 99b0b38806
commit b02ece20e7
No known key found for this signature in database
GPG Key ID: 3A289A85939877A4
2 changed files with 41 additions and 0 deletions

View File

@ -55,6 +55,9 @@ module Liquid
elsif @template_name_expr.is_a?(String)
partial = PartialCache.load(template, context: context, parse_context: parse_context)
template_name = partial.name
elsif template.nil?
template_name = @template_name_expr.respond_to?(:name) ? @template_name_expr.name : @template_name_expr
raise FileSystemError, "No such template '#{template_name}'"
else
raise ::ArgumentError, parse_context.locale.t("errors.argument.render")
end

View File

@ -480,6 +480,25 @@ class SnippetTest < Minitest::Test
assert_match("Expected end_of_string but found id", exception.message)
end
def test_render_with_non_existent_tag
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
{% snippet foo %}
{% render non_existent %}
{% endsnippet %}
{% render foo %}
LIQUID
expected = <<~TEXT
Liquid error (foo line 2): No such template 'non_existent'
TEXT
assert_equal(expected, template.render('errors' => ErrorDrop.new))
end
end
class RigidMode < SnippetTest
@ -956,6 +975,25 @@ class SnippetTest < Minitest::Test
assert_match("Expected a string or identifier, found 123", exception.message)
end
def test_render_with_non_existent_tag
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true, error_mode: :rigid)
{% snippet foo %}
{% render non_existent %}
{% endsnippet %}
{% render foo %}
LIQUID
expected = <<~TEXT
Liquid error (foo line 2): No such template 'non_existent'
TEXT
assert_equal(expected, template.render('errors' => ErrorDrop.new))
end
def test_render_with_no_identifier
template = "{% render %}"