mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Add handy context to strict parser error messages.
This commit is contained in:
+10
-2
@@ -35,11 +35,11 @@ module Liquid
|
|||||||
|
|
||||||
def parse_with_selected_parser(markup)
|
def parse_with_selected_parser(markup)
|
||||||
case @options[:error_mode] || Template.error_mode
|
case @options[:error_mode] || Template.error_mode
|
||||||
when :strict then strict_parse(markup)
|
when :strict then strict_parse_with_error_context(markup)
|
||||||
when :lax then lax_parse(markup)
|
when :lax then lax_parse(markup)
|
||||||
when :warn
|
when :warn
|
||||||
begin
|
begin
|
||||||
return strict_parse(markup)
|
return strict_parse_with_error_context(markup)
|
||||||
rescue SyntaxError => e
|
rescue SyntaxError => e
|
||||||
@warnings ||= []
|
@warnings ||= []
|
||||||
@warnings << e
|
@warnings << e
|
||||||
@@ -47,5 +47,13 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def strict_parse_with_error_context(markup)
|
||||||
|
strict_parse(markup)
|
||||||
|
rescue SyntaxError => e
|
||||||
|
e.message << " in \"#{markup.strip}\""
|
||||||
|
raise e
|
||||||
|
end
|
||||||
end # Tag
|
end # Tag
|
||||||
end # Liquid
|
end # Liquid
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ module Liquid
|
|||||||
@filters << [filtername, filterargs]
|
@filters << [filtername, filterargs]
|
||||||
end
|
end
|
||||||
p.consume(:end_of_string)
|
p.consume(:end_of_string)
|
||||||
|
rescue SyntaxError => e
|
||||||
|
e.message << " in \"{{#{markup}}}\""
|
||||||
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_filterargs(p)
|
def parse_filterargs(p)
|
||||||
|
|||||||
@@ -79,11 +79,23 @@ class ErrorHandlingTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_strict_error_messages
|
||||||
|
err = assert_raise(SyntaxError) do
|
||||||
|
Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :strict)
|
||||||
|
end
|
||||||
|
assert_equal 'Unexpected character = in "1 =! 2"', err.message
|
||||||
|
|
||||||
|
err = assert_raise(SyntaxError) do
|
||||||
|
Liquid::Template.parse('{{%%%}}', :error_mode => :strict)
|
||||||
|
end
|
||||||
|
assert_equal 'Unexpected character % in "{{%%%}}"', err.message
|
||||||
|
end
|
||||||
|
|
||||||
def test_warnings
|
def test_warnings
|
||||||
template = Liquid::Template.parse('{% if ~~~ %}derp{% else %}wat{% endif %}', :error_mode => :warn)
|
template = Liquid::Template.parse('{% if ~~~ %}derp{% else %}wat{% endif %}', :error_mode => :warn)
|
||||||
assert_equal 'wat', template.render
|
assert_equal 'wat', template.render
|
||||||
assert_equal 1, template.errors.size
|
assert_equal 1, template.errors.size
|
||||||
assert_equal 'Unexpected character ~.', template.errors.first.message
|
assert_equal 'Unexpected character ~ in "~~~"', template.errors.first.message
|
||||||
end
|
end
|
||||||
|
|
||||||
# Liquid should not catch Exceptions that are not subclasses of StandardError, like Interrupt and NoMemoryError
|
# Liquid should not catch Exceptions that are not subclasses of StandardError, like Interrupt and NoMemoryError
|
||||||
|
|||||||
Reference in New Issue
Block a user