-
-
Notifications
You must be signed in to change notification settings - Fork 809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raise Ransack::InvalidSearchError instead of ArgumentError on unknown conditions #1543
Raise Ransack::InvalidSearchError instead of ArgumentError on unknown conditions #1543
Conversation
@scarroll32 @deivid-rodriguez @gregmolnar I added this PR to resolve #1460. The tests are passing locally with rails 7.1. Could you take a look and allow CI tests to run? |
@sherifnada Thanks for this! Can you squash you commits? |
eaec59a
to
ecaf413
Compare
@gregmolnar done. Tests are succeeding locally but only when I run with rails 7.1:
Rails 7.2 is failing with the error that can be seen in CI logs |
is the rails 7.2 failure a known issue? it is happening on the main branch. happy to help with that one too @gregmolnar |
@sherifnada can you rebase your branch against main please? |
And can you also add a changelog entry please? |
@gregmolnar done |
Thank you! |
@sherifnada Can you also add a changelog entry please? |
df35652
to
6eeec9b
Compare
@gregmolnar apologies didn't push it correctly, should be good now |
Thank you! |
Summary
Currently, when Ransack encounters invalid search attributes (those not included in
ransackable_attributes
) while usingModel.ransack!()
it raises a genericArgumentError
. This makes it difficult to provide helpful feedback to an API consumer that their search params are incorrect.This PR adds a custom exception type
Ransack::InvalidSearchError
and raises it instead ofArgumentError
in cases where there are invalid attributes used in a search.✅ Backwards compatible
The solution in this PR should be perfectly backwards compatible. The new error inherits from ArgumentError, and the test cases assert this.
Problem
Assume I have the following code in my controller
As a result this will return 5xx to the consumer. But since this is a user error, i want to return 4xx.
I could fix this by adding the following:
but the problem is that I have to add this everywhere in my controllers.
I could make this cleaner by having a helper, for example defining a
rescuable_ransack
method to extend active record e.g:then using
rescuable_ransack
in my controllers:then add something to my application controller to catch all such exceptions:
which would work, but this also seems pretty useful to have in ransack itself. Hence this suggestion to add it to the library.
Proposed Solution
Add a specialized exception class (e.g.,
Ransack::InvalidSearchError
) that would be raised in cases whereignore_unknown_conditions
currently raises anArgumentError
. This would then allow me to add something in my ApplicationController like the following:This would be a very concise way to catch and handle such problems in controller actions.