mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
Handle disabled tags errors like other liquid errors (#1275)
This commit is contained in:
@@ -53,5 +53,6 @@ module Liquid
|
|||||||
UndefinedDropMethod = Class.new(Error)
|
UndefinedDropMethod = Class.new(Error)
|
||||||
UndefinedFilter = Class.new(Error)
|
UndefinedFilter = Class.new(Error)
|
||||||
MethodOverrideError = Class.new(Error)
|
MethodOverrideError = Class.new(Error)
|
||||||
|
DisabledError = Class.new(Error)
|
||||||
InternalError = Class.new(Error)
|
InternalError = Class.new(Error)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,14 +5,17 @@ module Liquid
|
|||||||
module Disableable
|
module Disableable
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
if context.tag_disabled?(tag_name)
|
if context.tag_disabled?(tag_name)
|
||||||
output << disabled_error_message
|
output << disabled_error(context)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def disabled_error_message
|
def disabled_error(context)
|
||||||
"#{tag_name} #{parse_context[:locale].t('errors.disabled.tag')}"
|
# raise then rescue the exception so that the Context#exception_renderer can re-raise it
|
||||||
|
raise DisabledError, "#{tag_name} #{parse_context[:locale].t('errors.disabled.tag')}"
|
||||||
|
rescue DisabledError => exc
|
||||||
|
context.handle_error(exc, line_number)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ class TagDisableableTest < Minitest::Test
|
|||||||
def test_disables_raw
|
def test_disables_raw
|
||||||
with_disableable_tags do
|
with_disableable_tags do
|
||||||
with_custom_tag('disable', DisableRaw) do
|
with_custom_tag('disable', DisableRaw) do
|
||||||
assert_template_result 'raw usage is not allowed in this contextfoo', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}'
|
output = Template.parse('{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}').render
|
||||||
|
assert_equal('Liquid error: raw usage is not allowed in this contextfoo', output)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -32,7 +33,8 @@ class TagDisableableTest < Minitest::Test
|
|||||||
def test_disables_echo_and_raw
|
def test_disables_echo_and_raw
|
||||||
with_disableable_tags do
|
with_disableable_tags do
|
||||||
with_custom_tag('disable', DisableRawEcho) do
|
with_custom_tag('disable', DisableRawEcho) do
|
||||||
assert_template_result 'raw usage is not allowed in this contextecho usage is not allowed in this context', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}'
|
output = Template.parse('{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}').render
|
||||||
|
assert_equal('Liquid error: raw usage is not allowed in this contextLiquid error: echo usage is not allowed in this context', output)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -127,7 +127,10 @@ class RenderTagTest < Minitest::Test
|
|||||||
'test_include' => '{% include "foo" %}'
|
'test_include' => '{% include "foo" %}'
|
||||||
)
|
)
|
||||||
|
|
||||||
assert_template_result('include usage is not allowed in this context', '{% render "test_include" %}')
|
exc = assert_raises(Liquid::DisabledError) do
|
||||||
|
Liquid::Template.parse('{% render "test_include" %}').render!
|
||||||
|
end
|
||||||
|
assert_equal('Liquid error: include usage is not allowed in this context', exc.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_will_not_render_inside_nested_sibling_tags
|
def test_includes_will_not_render_inside_nested_sibling_tags
|
||||||
@@ -137,7 +140,8 @@ class RenderTagTest < Minitest::Test
|
|||||||
'test_include' => '{% include "foo" %}'
|
'test_include' => '{% include "foo" %}'
|
||||||
)
|
)
|
||||||
|
|
||||||
assert_template_result('include usage is not allowed in this contextinclude usage is not allowed in this context', '{% render "nested_render_with_sibling_include" %}')
|
output = Liquid::Template.parse('{% render "nested_render_with_sibling_include" %}').render
|
||||||
|
assert_equal('Liquid error: include usage is not allowed in this contextLiquid error: include usage is not allowed in this context', output)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_tag_with
|
def test_render_tag_with
|
||||||
|
|||||||
Reference in New Issue
Block a user