Skip to content

Commit

Permalink
Handle TypeError from where filter gracefully (#9292)
Browse files Browse the repository at this point in the history
Merge pull request 9292
  • Loading branch information
ashmaroli committed Mar 24, 2023
1 parent 052f1bd commit d03742e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/jekyll/filters.rb
Expand Up @@ -441,6 +441,14 @@ def read_liquid_attribute(liquid_data, property)
property.split(".").reduce(liquid_data) do |data, key|
data.respond_to?(:[]) && data[key]
end
rescue TypeError => e
msg = if liquid_data.is_a?(Array)
"Error accessing object (#{liquid_data.to_s[0...20]}) with given key. Expected an " \
"integer but got #{property.inspect} instead."
else
e.message
end
raise e, msg
end

FLOAT_LIKE = %r!\A\s*-?(?:\d+\.?\d*|\.\d+)\s*\Z!.freeze
Expand Down
12 changes: 12 additions & 0 deletions test/test_filters.rb
Expand Up @@ -958,6 +958,18 @@ def to_liquid
results = @filter.where(SelectDummy.new, "obj", "1 == 1")
assert_equal [], results
end

should "gracefully handle invalid property type" do
hash = {
"members" => { "name" => %w(John Jane Jimmy) },
"roles" => %w(Admin Recruiter Manager),
}
err = assert_raises(TypeError) { @filter.where(hash, "name", "Jimmy") }
truncatd_arr_str = hash["roles"].to_liquid.to_s[0...20]
msg = "Error accessing object (#{truncatd_arr_str}) with given key. Expected an integer " \
'but got "name" instead.'
assert_equal msg, err.message
end
end

context "where_exp filter" do
Expand Down

0 comments on commit d03742e

Please sign in to comment.