Skip to content

Commit

Permalink
render template name with Liquid Syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Mar 2, 2023
1 parent 3ff4170 commit 1d97389
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/liquid/partial_cache.rb
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
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
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

0 comments on commit 1d97389

Please sign in to comment.