Resource limits: Don't raise Error but render error message (but abort after first error)

This commit is contained in:
Florian Weingarten
2013-05-31 09:25:25 -04:00
parent 8760b5e8c4
commit 9075b428b1
3 changed files with 22 additions and 7 deletions
+2 -2
View File
@@ -108,9 +108,9 @@ module Liquid
token_output = (token.respond_to?(:render) ? token.render(context) : token)
context.resource_limits[:render_length_current] += (token_output.respond_to?(:length) ? token_output.length : 1)
raise Liquid::MemoryError, context.resource_limits if context.resource_limits_reached?
raise MemoryError.new("Memory limits exceeded") if context.resource_limits_reached?
output << token_output
rescue Liquid::MemoryError => e
rescue MemoryError => e
raise e
rescue ::StandardError => e
output << (context.handle_error(e))
+3 -1
View File
@@ -120,9 +120,11 @@ module Liquid
begin
# render the nodelist.
# for performance reasons we get a array back here. join will make a string out of it
# for performance reasons we get an array back here. join will make a string out of it.
result = @root.render(context)
result.respond_to?(:join) ? result.join : result
rescue Liquid::MemoryError => e
context.handle_error(e)
ensure
@errors = context.errors
end
+17 -4
View File
@@ -72,26 +72,39 @@ class TemplateTest < Test::Unit::TestCase
@global = nil
end
def test_resource_limits
def test_resource_limits_render_length
t = Template.parse("0123456789")
t.resource_limits = { :render_length_limit => 5 }
assert_raises(Liquid::MemoryError) { t.render() }
assert_equal "Liquid error: Memory limits exceeded", t.render()
t.resource_limits = { :render_length_limit => 10 }
assert_equal "0123456789", t.render()
assert_not_nil t.resource_limits[:render_length_current]
end
def test_resource_limits_render_score
t = Template.parse("{% for a in (1..10) %} {% for a in (1..10) %} foo {% endfor %} {% endfor %}")
t.resource_limits = { :render_score_limit => 50 }
assert_equal "Liquid error: Memory limits exceeded", t.render()
t = Template.parse("{% for a in (1..100) %} foo {% endfor %}")
t.resource_limits = { :render_score_limit => 50 }
assert_raises(Liquid::MemoryError) { t.render() }
assert_equal "Liquid error: Memory limits exceeded", t.render()
t.resource_limits = { :render_score_limit => 200 }
assert_equal (" foo " * 100), t.render()
assert_not_nil t.resource_limits[:render_score_current]
end
def test_resource_limits_assign_score
t = Template.parse("{% assign foo = 42 %}{% assign bar = 23 %}")
t.resource_limits = { :assign_score_limit => 1 }
assert_raises(Liquid::MemoryError) { t.render() }
assert_equal "Liquid error: Memory limits exceeded", t.render()
t.resource_limits = { :assign_score_limit => 2 }
assert_equal "", t.render()
assert_not_nil t.resource_limits[:assign_score_current]
end
def test_resource_limits_aborts_rendering_after_first_error
t = Template.parse("{% for a in (1..100) %} foo1 {% endfor %} bar {% for a in (1..100) %} foo2 {% endfor %}")
t.resource_limits = { :render_score_limit => 50 }
assert_equal "Liquid error: Memory limits exceeded", t.render()
end
end # TemplateTest