Custom Exceptions and Messages #800
saturnflyer
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
In the |
Beta Was this translation helpful? Give feedback.
1 reply
-
I wrapped up this and other features into https://github.com/SOFware/pundit-plus |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've seen some discussion around handling custom exceptions and I want to find out if I can contribute to the effort.
I'm currently patching Pundit in my application like this:
Because the
Pundit.authorize
method has no place to specify a custom exception class, the only way to hook into it is to alter theraise
method.This patch allows me to define an
exception_from
method in my policies which can return alternative exception classes.This is how you can use it:
The upside of this is that there's little chance of this causing trouble if Pundit changes upstream from my patch.
One downside is that the
else
case needs to always return the Pundit::NotAuthorizedError, or the ApplicationPolicy could just define this method to have a default and we'd always callsuper
in theelse
clause.Or my ApplicationPolicy could instead add an alternative method path to ensure that it works if people forget to add the
super
call.For example:
There seems to be no currently blessed way to handle custom exceptions and because policies are just simple objects that have minimal requirements, this would add some additional overhead to what those objects need to do. Checking
respond_to?
isn't ideal but the alternative would be that the policies would need some new requirement to specify some method to return the exception class.The generator could define a method like
exception_from
orcustom_exception
(or whatever makes sense) and to avoid regularrespond_to?
checking that would require all existing projects to define the appropriate method to work with some version of pundit that required it. I'm not sure if that's an acceptable addition to the required API for policy objects.This is what I'm currently doing in my project and I wanted to share it and get feedback if something like this would be an acceptable addition to Pundit.
Beta Was this translation helpful? Give feedback.
All reactions