From b7ee1a21760ff83f52e7a9373332547e41129a30 Mon Sep 17 00:00:00 2001 From: Philibert Dugas Date: Fri, 9 Sep 2016 20:05:15 -0400 Subject: [PATCH] Fixing #697 with better exception When including a template which is not defined, the exception raised is *undefined method `split` for nil:NilClass* This occurs for a scenario like the following: `{% include nil %}` or `{% include undefined-var %}` Making the code raise an argument error to allow better understanding of the include error --- lib/liquid/locales/en.yml | 2 ++ lib/liquid/tags/include.rb | 3 ++- test/integration/tags/include_tag_test.rb | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/liquid/locales/en.yml b/lib/liquid/locales/en.yml index e69eb3e..9a259bf 100644 --- a/lib/liquid/locales/en.yml +++ b/lib/liquid/locales/en.yml @@ -22,3 +22,5 @@ tag_never_closed: "'%{block_name}' tag was never closed" meta_syntax_error: "Liquid syntax error: #{e.message}" table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3" + argument: + include: "Argument error in tag 'include' - Illegal template name" diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index ba970cc..9234eb4 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -42,8 +42,9 @@ module Liquid def render(context) template_name = context.evaluate(@template_name_expr) - partial = load_cached_partial(template_name, context) + raise ArgumentError.new(options[:locale].t("errors.argument.include")) unless template_name + partial = load_cached_partial(template_name, context) context_variable_name = template_name.split('/'.freeze).last variable = if @variable_name_expr diff --git a/test/integration/tags/include_tag_test.rb b/test/integration/tags/include_tag_test.rb index 87dfe80..7b4e3f6 100644 --- a/test/integration/tags/include_tag_test.rb +++ b/test/integration/tags/include_tag_test.rb @@ -217,6 +217,17 @@ class IncludeTagTest < Minitest::Test end end + def test_render_raise_argument_error_when_template_is_undefined + assert_raises(Liquid::ArgumentError) do + template = Liquid::Template.parse('{% include undefined_variable %}') + template.render! + end + assert_raises(Liquid::ArgumentError) do + template = Liquid::Template.parse('{% include nil %}') + template.render! + end + end + def test_including_via_variable_value assert_template_result "from TestFileSystem", "{% assign page = 'pick_a_source' %}{% include page %}"