У меня проблема с правильной обработкой исключения в промежуточном программном обеспечении Django.Мое исключение:
from rest_framework.exceptions import APIException
from rest_framework.status import HTTP_403_FORBIDDEN
class MyProfileAuthorizationError(APIException):
def __init__(self, msg):
APIException.__init__(self, msg)
self.status_code = HTTP_403_FORBIDDEN
self.message = msg
И мое промежуточное ПО:
class PatchRequestUserWithProfile:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request, *args, **kwargs):
patch_request_for_nonanon_user(request)
if not request.user.profile:
raise MyProfileAuthorizationError("You are not allowed to use this profile.")
response = self.get_response(request)
return response
И это исключение выдает 500 вместо 403. Как я могу это исправить?