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 e4ecca4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/liquid/partial_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ 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)
partial.name ||= template_name
begin
partial = template.parse(source, parse_context)
partial.name ||= template_name
rescue Liquid::Error => e
# if Liquid#Parse fails, we want to add the template name to the error message
e.template_name = template&.name || template_name
raise e
end

cached_partials[template_name] = partial
ensure
Expand Down
3 changes: 3 additions & 0 deletions lib/liquid/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def parse(source, options = {})
parse_context = configure_options(options)
tokenizer = parse_context.new_tokenizer(source, start_line_number: @line_numbers && 1)
@root = Document.parse(tokenizer, parse_context)
self.name ||= options[:name]
self
end

Expand Down Expand Up @@ -189,6 +190,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,
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_actual_filenmae
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,
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_stack_level_error_is_raised_with_actual_filenmae
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,
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 e4ecca4

Please sign in to comment.