Skip to content
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

Custom Error Messages Not Working #13

Open
virtualbrown opened this issue May 2, 2018 · 1 comment
Open

Custom Error Messages Not Working #13

virtualbrown opened this issue May 2, 2018 · 1 comment

Comments

@virtualbrown
Copy link

DRF has a built-in way of providing custom error messages.
However, these error messages are currently ignored by 'rest_condition' classes. Instead, the default error message is always used.

In DRF, you can specify a class level attribute called message, that gets used as the error message if the permission check fails.

http://www.django-rest-framework.org/api-guide/permissions/#custom-permissions

e.g.

from rest_framework import permissions

class CustomerAccessPermission(permissions.BasePermission):
   message = 'Adding customers not allowed.'

def has_permission(self, request, view):
     ...

To fix this, It seems evaluate_permissions(....) in permissions.py could be enhanced to read the message attribute from the condition instance and set its own message attribute to that.

e.g. permissions.py

def evaluate_permissions(....)

    if reduced_result is not _NONE:

        # read the error message from the custom permissions class and set it here so DRF
       # can pick it up.
       if hasattr(condition.__class__, 'message'):
           Condition.message = condition.__class__.message

         return not reduced_result if self.negated else reduced_result
@usmanimtiaz
Copy link

do we have any word on if this has bee updated or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants