mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-17 01:40:42 -07:00
Raise Liquid::SyntaxError instead of NoMethodError for invalid range (#1607)
This commit is contained in:
@@ -8,7 +8,17 @@ module Liquid
|
||||
if start_obj.respond_to?(:evaluate) || end_obj.respond_to?(:evaluate)
|
||||
new(start_obj, end_obj)
|
||||
else
|
||||
start_obj.to_i..end_obj.to_i
|
||||
begin
|
||||
start_obj.to_i..end_obj.to_i
|
||||
rescue NoMethodError
|
||||
invalid_expr = start_markup unless start_obj.respond_to?(:to_i)
|
||||
invalid_expr ||= end_markup unless end_obj.respond_to?(:to_i)
|
||||
if invalid_expr
|
||||
raise Liquid::SyntaxError, "Invalid expression type '#{invalid_expr}' in range expression"
|
||||
end
|
||||
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -29,6 +29,11 @@ class ExpressionTest < Minitest::Test
|
||||
def test_range
|
||||
assert_equal(1..2, parse_and_eval("(1..2)"))
|
||||
assert_equal(3..4, parse_and_eval(" ( 3 .. 4 ) "))
|
||||
|
||||
exc = assert_raises(Liquid::SyntaxError) { Liquid::Expression.parse("(false..true)") }
|
||||
assert_equal("Liquid syntax error: Invalid expression type 'false' in range expression", exc.message)
|
||||
exc = assert_raises(Liquid::SyntaxError) { Liquid::Expression.parse("((1..2)..3)") }
|
||||
assert_equal("Liquid syntax error: Invalid expression type '(1..2)' in range expression", exc.message)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user