-
Notifications
You must be signed in to change notification settings - Fork 635
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
Do not use NotImplementedError #776
Conversation
Thank you for the PR! Please update the changelog. |
I worry that In my own code, I’ve used my own class SubclassResponsibilityError < StandardError
def initialize(klass, method_name)
super("Subclasses of #{klass} must implement the ##{method_name} method.")
end
end Would this make sense? It would mean adding a custom exception to the generator, which might not be what we want. In any case, I do think |
@denisdefreyne I understand your worry, and it's a valid point. 😊 Taking that into consideration, and weighing that against adding a custom exception to the generator, together with the fact that the error message is descriptive, I think it's worth seeing how this flies in the wild. It's generated code, so if this turns out to be confusing for people then 1) we can adjust it and 2) nobody needs to wait for a new release. |
6acc83f
to
00188af
Compare
What is a bit odd about this to me is that https://ruby-doc.org/core-3.0.0/NoMethodError.html
I'm not saying
But maybe it maps closer to how it's used in this context? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
My thinking is that this error is for developers and not something you would expect to see runtime. We could have our own error (MethodNotImplemented = Class.new(Pundit::Error)
) and raise that, but I don't think increasing our API surface area (another public name) is worth it for this.
Main benefit here is no longer inheriting from ScriptError
, which an unimplemented resolve
definitely is not.
Instead use NoMethodError as to indicate that the method should be defined in the class Closes varvet#775
00188af
to
6eec6c6
Compare
FYI @hallelujah I updated this branch with a rebase, because |
Strictly speaking, this is a BR break. See the classic https://www.honeybadger.io/blog/understanding-the-ruby-exception-hierarchy/. |
This change affects a generator for the ApplicationPolicy, which is typically run once in the lifetime of an application. If you've set up Pundit in your app already, this won't affect you. If you're setting up Pundit fresh in a new application, then yes this might raise a different error than what you're used to. If you make a habit of Regardless, you should be able to |
Of course, totally missed that it's in a generator. 🤦🏼 |
[Pundit have recently changed](varvet/pundit#776) to using `NoMethodError`, which breaks our spec - the main benefit of this change is that the new error extends from `StandardError` so in theory is easier to catch; however, it seems the community is divided as aside from the different superclass the existing error is more accurate since `NoMethodError` is meant to be thrown when the class does not `respond_to?` the method in question and that is not true. I personally would like to keep using `NotImplementedError` though I'm happy to change if someone does prove a real-world situation where it's more burdson to catch (or that our error monitoring won't catch it), but I feel that should be a separate conversation - so this PR is changing it back to maintain our current status quo until such time that we do decide to change.
Instead use
NoMethodError
as to indicate that the method should be defined in the classCloses #775
To do
PS: Thank you for contributing to Pundit ❤️