Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render template name with Liquid Syntax errors #1695

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/liquid/partial_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ def self.load(template_name, context:, parse_context:)
template_factory = context.registers[:template_factory]
template = template_factory.for(template_name)

partial = template.parse(source, parse_context)
begin
partial = template.parse(source, parse_context)
rescue Liquid::Error => e
e.template_name = template&.name || template_name
raise e
end

partial.name ||= template_name

cached_partials[template_name] = partial
Expand Down
2 changes: 2 additions & 0 deletions lib/liquid/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def render(*args)
@profiler = context.profiler = Liquid::Profiler.new
end

context.template_name ||= name

begin
# render the nodelist.
@root.render_to_output_buffer(context, output || +'')
Expand Down
77 changes: 77 additions & 0 deletions test/integration/error_handling_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,81 @@ def test_bug_compatible_silencing_of_errors_in_blank_nodes
output = Liquid::Template.parse("{% assign x = 0 %}{% if 1 < '2' %}{% assign x = 3 %}{% endif %}{{ x }}").render
assert_equal("0", output)
end

def test_syntax_error_is_raised_with_template_name
file_system = StubFileSystem.new("snippet" => "1\n2\n{{ 1")

context = Liquid::Context.build(
registers: { file_system: file_system },
)

template = Template.parse(
'{% render "snippet" %}',
line_numbers: true,
)
template.name = "template/index"

assert_equal(
"Liquid syntax error (snippet line 3): Variable '{{' was not properly terminated with regexp: /\\}\\}/",
template.render(context),
)
end

def test_syntax_error_is_raised_with_template_name_from_template_factory
file_system = StubFileSystem.new("snippet" => "1\n2\n{{ 1")

context = Liquid::Context.build(
registers: {
file_system: file_system,
template_factory: StubTemplateFactory.new,
},
)

template = Template.parse(
'{% render "snippet" %}',
line_numbers: true,
)
template.name = "template/index"

assert_equal(
"Liquid syntax error (some/path/snippet line 3): Variable '{{' was not properly terminated with regexp: /\\}\\}/",
template.render(context),
)
end

def test_error_is_raised_during_parse_with_template_name
depth = Liquid::Block::MAX_DEPTH + 1
code = "{% if true %}" * depth + "rendered" + "{% endif %}" * depth

template = Template.parse("{% render 'snippet' %}", line_numbers: true)

context = Liquid::Context.build(
registers: {
file_system: StubFileSystem.new("snippet" => code),
template_factory: StubTemplateFactory.new,
},
)

assert_equal("Liquid error (some/path/snippet line 1): Nesting too deep", template.render(context))
end

def test_internal_error_is_raised_with_template_name
template = Template.new
template.parse(
"{% render 'snippet' %}",
line_numbers: true,
)
template.name = "template/index"

context = Liquid::Context.build(
registers: {
file_system: StubFileSystem.new({}),
},
)

assert_equal(
"Liquid error (template/index line 1): internal",
template.render(context),
)
end
end