Skip to content

Custom Error Messages Not Working #13

@virtualbrown

Description

@virtualbrown

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions