Skip to content

Handle TypeError from where filter gracefully#9292

Merged
jekyllbot merged 5 commits intojekyll:masterfrom
ashmaroli:handle-where-filter-type-error
Mar 24, 2023
Merged

Handle TypeError from where filter gracefully#9292
jekyllbot merged 5 commits intojekyll:masterfrom
ashmaroli:handle-where-filter-type-error

Conversation

@ashmaroli
Copy link
Copy Markdown
Member

@ashmaroli ashmaroli commented Feb 5, 2023

  • This is a 🐛 bug fix.
  • I've added tests.
  • The test suite passes locally.

Summary

Invoking Array#[] with a non-integer argument raises no implicit conversion of String into Integer (TypeError) which is too technical and vague for end-users. Therefore, surface a more comprehensive message with additional context.

Context

Fixes #9289

TODO

Perhaps in a future pull request:

  • Let users know the error occurred via a Liquid filter or tag.

@ashmaroli ashmaroli requested review from mattr- and parkr February 5, 2023 18:49
"members" => { "name" => %w(John Jane Jimmy) },
"roles" => %w(Admin Recruiter Manager),
}
assert_raises(TypeError) { @filter.where(hash, "name", "Jimmy") }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think we want to add an assertion on the message here; this would pass without your change I think, so there's no way to know if we broke it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an assertion.

"Error accessing object (#{liquid_data}) with given key. Expected an integer but " \
"got #{property.inspect} instead"
else
"Error accessing object with key #{property}. #{e.message}"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this line is untested?

property.split(".").reduce(liquid_data) do |data, key|
data.respond_to?(:[]) && data[key]
end
rescue StandardError => e
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is StandardError too broad? Should we keep it to TypeError or only catch when we see the error message you mention in the PR description?

@ashmaroli ashmaroli requested a review from parkr March 23, 2023 14:59
end
rescue TypeError => e
msg = if liquid_data.is_a?(Array)
"Error accessing object (#{liquid_data}) with given key. Expected an integer but " \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Printing the whole object could be an issue. Imagine all of site.github; it would flood the terminal output with superfluous data. How would you feel about truncating the liquid_data output?

Copy link
Copy Markdown
Member Author

@ashmaroli ashmaroli Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Good catch once again.
In theory, we could include Liquid:: StandardFilters and pass the liquid_data onto the upstream truncate filter method.
Do you think that's a good idea? Do you foresee any side-effects from this?

Update: Nevermind. I truncated directly instead.

@ashmaroli
Copy link
Copy Markdown
Member Author

@jekyllbot: merge +fix

@jekyllbot jekyllbot merged commit d03742e into jekyll:master Mar 24, 2023
jekyllbot added a commit that referenced this pull request Mar 24, 2023
github-actions bot pushed a commit that referenced this pull request Mar 24, 2023
Ashwin Maroli: Handle TypeError from `where` filter gracefully (#9292)

Merge pull request 9292
@jekyll jekyll locked and limited conversation to collaborators Mar 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unexpected crash: filters.rb:442:in `read_liquid_attribute': no implicit conversion of String into Integer

3 participants