-
Notifications
You must be signed in to change notification settings - Fork 50
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
Support dynamic response message and code #45
Comments
Django REST Framework offers a way to do custom exception handlering: If that doesn't provide a solution for you, I'm glad to find out more about your use case and the possibilities for supporting it. |
@TheNightmareX I have some very much work-in-progress code that does something similar to what you described. So far from a drop in solution, but I mention in hopes it might be useful. It's mostly a matter of extending drf's exception handling as @rsinger86 mentioned. My WIP branch is messy, but the main bits are in: ansible/galaxy_ng@master...alikins:add_access_policy_info_to_exc_context#diff-7fd05104d358955e7a768580ce849c06ddba04aa1516b04a339725a60ee0df7d ansible/galaxy_ng@master...alikins:add_access_policy_info_to_exc_context#diff-4cb0675d6dad1320a78aab94819a62fcae7a58dd8384cff749a65ab983955f10 Most of the rest is some munging of the AccessPolicy to keep track of where/why/what conditions were 'denied'. |
Another approach I've used in the past on a different project is to [ab]use the fact that Permission checks return a boolean. But it doesn't have to be True/False, but could be an object that acts like a bool (ie, an instance of a class that defines a bool()). So instead of returning False when the AccessPolicy permsion check fails, it could return a PermissionDenied. Something very vaguely like... class PermissionDenied:
def __init__(self, message=None, code=None, reason=None, **kwargs):
self.message = message
self.code = code or DEFAULT_CODE
self.reason = reason
def __bool__(self):
return False Then up the stack, instead of getting a False, you would get a PermissionDenied with extra context. It's little weird, but can be handy for complicated error cases. It's more or less a slightly tweaked version of One thing possible with the BoolAnd and BoolOr is they could track the sub Ops, and potentially present |
@rsinger86 the use case can be like this. A have a Policy Statement:
I'm doing validation and if
IMHO policy evaluation can check from top to bottom but on the first condition that will return False should stop, there is no point to evaluate the further conditions in the queue. I can order them in a proper way on the list. In that scenario, if a user is not an organization member will return False and PermissionDenied in view. |
I need to change the response message and code dynamically to tell the front-end why it happened.
The message and the code can be defined by defining the
message
andcode
attribute, but they are static and it seems that there is no implementation to change them dynamically.The text was updated successfully, but these errors were encountered: