rest_condition icon indicating copy to clipboard operation
rest_condition copied to clipboard

Custom Error Messages Not Working

Open virtualbrown opened this issue 6 years ago • 1 comments

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

virtualbrown avatar May 02 '18 15:05 virtualbrown

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

usmanimtiaz avatar Sep 10 '20 08:09 usmanimtiaz