Merge pull request #1514 from maul-esel/tag-errors

Consistent error handling in Liquid tags
This commit is contained in:
Matt Rogers 2013-09-30 20:54:42 -07:00
commit 9d4f9169e9
4 changed files with 33 additions and 32 deletions

View File

@ -12,7 +12,15 @@ module Jekyll
gist_id, filename = tag_contents[0], tag_contents[1] gist_id, filename = tag_contents[0], tag_contents[1]
gist_script_tag(gist_id, filename) gist_script_tag(gist_id, filename)
else else
"Error parsing gist id" raise ArgumentError.new <<-eos
Syntax error in tag 'gist' while parsing the following markup:
#{@markup}
Valid syntax:
for public gists: {% gist 1234567 %}
for private gists: {% gist user/1234567 %}
eos
end end
end end

View File

@ -77,14 +77,10 @@ eos
def render(context) def render(context)
dir = File.join(context.registers[:site].source, INCLUDES_DIR) dir = File.join(context.registers[:site].source, INCLUDES_DIR)
if error = validate_dir(dir, context.registers[:site].safe) validate_dir(dir, context.registers[:site].safe)
return error
end
file = File.join(dir, @file) file = File.join(dir, @file)
if error = validate_file(dir, context.registers[:site].safe) validate_file(file, context.registers[:site].safe)
return error
end
partial = Liquid::Template.parse(source(file, context)) partial = Liquid::Template.parse(source(file, context))
@ -96,15 +92,15 @@ eos
def validate_dir(dir, safe) def validate_dir(dir, safe)
if File.symlink?(dir) && safe if File.symlink?(dir) && safe
"Includes directory '#{dir}' cannot be a symlink" raise IOError.new "Includes directory '#{dir}' cannot be a symlink"
end end
end end
def validate_file(file, safe) def validate_file(file, safe)
if !File.exists?(file) if !File.exists?(file)
"Included file '#{@file}' not found in '#{INCLUDES_DIR}' directory" raise IOError.new "Included file '#{@file}' not found in '#{INCLUDES_DIR}' directory"
elsif File.symlink?(file) && safe elsif File.symlink?(file) && safe
"The included file '#{INCLUDES_DIR}/#{@file}' should not be a symlink" raise IOError.new "The included file '#{INCLUDES_DIR}/#{@file}' should not be a symlink"
end end
end end

View File

@ -50,9 +50,11 @@ module Jekyll
end end
end end
puts "ERROR: post_url: \"#{@orig_post}\" could not be found" raise ArgumentError.new <<-eos
Could not find post "#{@orig_post}" in tag 'post_url'.
return "#" Make sure the post exists and the name is correct.
eos
end end
end end
end end

View File

@ -19,7 +19,7 @@ class TestTags < Test::Unit::TestCase
payload = { "pygments_prefix" => @converter.pygments_prefix, payload = { "pygments_prefix" => @converter.pygments_prefix,
"pygments_suffix" => @converter.pygments_suffix } "pygments_suffix" => @converter.pygments_suffix }
@result = Liquid::Template.parse(content).render(payload, info) @result = Liquid::Template.parse(content).render!(payload, info)
@result = @converter.convert(@result) @result = @converter.convert(@result)
end end
@ -273,22 +273,19 @@ CONTENT
end end
end end
context "when invalid" do should "raise ArgumentError when invalid" do
setup do @gist = "mattr-24081a1d93d2898ecf0f"
@gist = "mattr-24081a1d93d2898ecf0f" @filename = "myfile.ext"
@filename = "myfile.ext" content = <<CONTENT
content = <<CONTENT
--- ---
title: My Cool Gist title: My Cool Gist
--- ---
{% gist #{@gist} #{@filename} %} {% gist #{@gist} #{@filename} %}
CONTENT CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "write script tag with specific file in gist" do assert_raise ArgumentError do
assert_match "Error parsing gist id", @result create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end end
end end
end end
@ -313,7 +310,7 @@ CONTENT
end end
context "with blank gist id" do context "with blank gist id" do
setup do should "raise ArgumentError" do
content = <<CONTENT content = <<CONTENT
--- ---
title: My Cool Gist title: My Cool Gist
@ -321,16 +318,15 @@ title: My Cool Gist
{% gist %} {% gist %}
CONTENT CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "output error message" do assert_raise ArgumentError do
assert_match "Error parsing gist id", @result create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
end end
end end
context "with invalid gist id" do context "with invalid gist id" do
setup do should "raise ArgumentError" do
invalid_gist = 'invalid' invalid_gist = 'invalid'
content = <<CONTENT content = <<CONTENT
--- ---
@ -339,11 +335,10 @@ title: My Cool Gist
{% gist #{invalid_gist} %} {% gist #{invalid_gist} %}
CONTENT CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "output error message" do assert_raise ArgumentError do
assert_match "Error parsing gist id", @result create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
end end
end end
end end