Skip to content

Commit

Permalink
Raise Liquid::SyntaxError instead of NoMethodError for invalid range (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith authored Aug 25, 2022
1 parent 98e146e commit eb89f22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/liquid/range_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ def self.parse(start_markup, end_markup)
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

Expand Down
5 changes: 5 additions & 0 deletions test/integration/expression_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def test_float
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
Expand Down

0 comments on commit eb89f22

Please sign in to comment.