Merge pull request #1514 from maul-esel/tag-errors
Consistent error handling in Liquid tags
This commit is contained in:
commit
9d4f9169e9
|
@ -12,7 +12,15 @@ module Jekyll
|
|||
gist_id, filename = tag_contents[0], tag_contents[1]
|
||||
gist_script_tag(gist_id, filename)
|
||||
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
|
||||
|
||||
|
|
|
@ -77,14 +77,10 @@ eos
|
|||
|
||||
def render(context)
|
||||
dir = File.join(context.registers[:site].source, INCLUDES_DIR)
|
||||
if error = validate_dir(dir, context.registers[:site].safe)
|
||||
return error
|
||||
end
|
||||
validate_dir(dir, context.registers[:site].safe)
|
||||
|
||||
file = File.join(dir, @file)
|
||||
if error = validate_file(dir, context.registers[:site].safe)
|
||||
return error
|
||||
end
|
||||
validate_file(file, context.registers[:site].safe)
|
||||
|
||||
partial = Liquid::Template.parse(source(file, context))
|
||||
|
||||
|
@ -96,15 +92,15 @@ eos
|
|||
|
||||
def validate_dir(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
|
||||
|
||||
def validate_file(file, safe)
|
||||
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
|
||||
"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
|
||||
|
||||
|
|
|
@ -50,9 +50,11 @@ module Jekyll
|
|||
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
|
||||
|
|
|
@ -19,7 +19,7 @@ class TestTags < Test::Unit::TestCase
|
|||
payload = { "pygments_prefix" => @converter.pygments_prefix,
|
||||
"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)
|
||||
end
|
||||
|
||||
|
@ -273,8 +273,7 @@ CONTENT
|
|||
end
|
||||
end
|
||||
|
||||
context "when invalid" do
|
||||
setup do
|
||||
should "raise ArgumentError when invalid" do
|
||||
@gist = "mattr-24081a1d93d2898ecf0f"
|
||||
@filename = "myfile.ext"
|
||||
content = <<CONTENT
|
||||
|
@ -284,11 +283,9 @@ CONTENT
|
|||
|
||||
{% gist #{@gist} #{@filename} %}
|
||||
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_match "Error parsing gist id", @result
|
||||
assert_raise ArgumentError do
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -313,7 +310,7 @@ CONTENT
|
|||
end
|
||||
|
||||
context "with blank gist id" do
|
||||
setup do
|
||||
should "raise ArgumentError" do
|
||||
content = <<CONTENT
|
||||
---
|
||||
title: My Cool Gist
|
||||
|
@ -321,16 +318,15 @@ title: My Cool Gist
|
|||
|
||||
{% gist %}
|
||||
CONTENT
|
||||
|
||||
assert_raise ArgumentError do
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
|
||||
should "output error message" do
|
||||
assert_match "Error parsing gist id", @result
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid gist id" do
|
||||
setup do
|
||||
should "raise ArgumentError" do
|
||||
invalid_gist = 'invalid'
|
||||
content = <<CONTENT
|
||||
---
|
||||
|
@ -339,11 +335,10 @@ title: My Cool Gist
|
|||
|
||||
{% gist #{invalid_gist} %}
|
||||
CONTENT
|
||||
|
||||
assert_raise ArgumentError do
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
|
||||
should "output error message" do
|
||||
assert_match "Error parsing gist id", @result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue