Custom Exception


imports

from rest_framework.exceptions import PermissionDenied
from rest_framework import status


define class

class CustomExcpetion(PermissionDenied):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = "Custom Exception Message"
default_code = 'invalid'

def __init__(self, detail, status_code=None):
self.detail = detail
if status_code is not None:
self.status_code = status_code


use in methods

raise CustomExcpetion(
detail=response.json(),
status_code=response.status_code
)



date:Nov. 18, 2023