from rest_framework.views import exception_handler from rest_framework.exceptions import AuthenticationFailed from rest_framework_simplejwt.exceptions import InvalidToken
defcustom_exception_handler(exc, context): # Call REST framework's default exception handler first, # to get the standard error response. response = exception_handler(exc, context) # Now add the HTTP status code to the response. if response isnotNone: if type(exc) == AuthenticationFailed: response.data['status_code'] = 401 response.data['detail'] = '用户名或者密码错误' elif type(exc) == InvalidToken: response.data['status_code'] = 5008 response.data['detail'] = 'Token is invalid or expired' else: response.data['status_code'] = response.status_code