Custom Exceptions and Messages #800
saturnflyer
started this conversation in
Ideas
Replies: 2 comments 1 reply
|
In the |
1 reply
|
I wrapped up this and other features into https://github.com/SOFware/pundit-plus |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
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.authorizemethod has no place to specify a custom exception class, the only way to hook into it is to alter theraisemethod.This patch allows me to define an
exception_frommethod 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
elsecase needs to always return the Pundit::NotAuthorizedError, or the ApplicationPolicy could just define this method to have a default and we'd always callsuperin theelseclause.Or my ApplicationPolicy could instead add an alternative method path to ensure that it works if people forget to add the
supercall.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_fromorcustom_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.
All reactions