render error message with actual template path

This commit is contained in:
Michael Go
2023-02-27 11:01:21 -04:00
parent e650dc4195
commit 0fe4a5d144
5 changed files with 63 additions and 3 deletions
+13
View File
@@ -356,4 +356,17 @@ class IncludeTagTest < Minitest::Test
partials: { 'break' => "{% break %}" },
)
end
def test_include_tag_renders_actual_template_name_for_error
original_file_system = Liquid::Template.file_system
Liquid::Template.file_system = MemoryFileSystem.new(
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}",
)
template = Liquid::Template.parse("{% include 'foo' with errors %}", line_numbers: true)
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render('errors' => ErrorDrop.new))
ensure
Liquid::Template.file_system = original_file_system
end
end # IncludeTagTest
+13
View File
@@ -264,4 +264,17 @@ class RenderTagTest < Minitest::Test
},
)
end
def test_render_tag_renders_actual_template_name_for_error
original_file_system = Liquid::Template.file_system
Liquid::Template.file_system = MemoryFileSystem.new(
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}",
)
template = Liquid::Template.parse("{% render 'foo' with errors %}", line_numbers: true)
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render('errors' => ErrorDrop.new))
ensure
Liquid::Template.file_system = original_file_system
end
end
+22
View File
@@ -214,3 +214,25 @@ class StubTemplateFactory
Liquid::Template.new
end
end
class MemoryFileSystem
def initialize(values)
# values is a hash of template_path => template_source
@snippets = {}
values.each do |file_path, source|
key = file_path.split('/').last.delete_suffix(".liquid")
@snippets[key] = {
source: source,
actual_template_name: file_path,
}
end
end
def read_template_file(template_name)
@snippets[template_name][:source]
end
def actual_template_name(template_name)
@snippets[template_name][:actual_template_name]
end
end