我使用Django Rest Framework 3.2.3(DRF)和Django Rest Framework JWT 1.7.2(DRF-JWT,https://github.com/GetBlimp/django-rest-framework-jwt)創建登錄標記。Django Rest Framework中的ValidationError JWT不使用自定義異常處理程序
當從400到202發佈JWT時,我需要更改無效憑據的狀態代碼(僅供參考,我的客戶端無法讀取非200響應的主體)。我使用自定義異常處理程序,通過Django的REST框架描述來實現它:http://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling
RESTAPI/custom_exception.py
from rest_framework.views import exception_handler
from rest_framework import status
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
print ('Exception raised: ' + str(response.status_code))
# Now add the HTTP status code to the response.
if response is not None:
if response.status_code != status.HTTP_403_FORBIDDEN:
response.status_code = status.HTTP_202_ACCEPTED
return response
而且在配置:
'EXCEPTION_HANDLER': 'restapi.custom_exception.custom_exception_handler',
的DRF-當使用無效憑證時,JWT應該引發ValidationError。在向JWT token-auth接口發佈無效憑證時,我仍然收到400個「錯誤請求」響應代碼。
與其他每個DRF接口一樣,我按預期獲得了202狀態碼。
我該如何獲得DRF-JWT的ValidationErrors自定義異常處理程序?
如果'提高ValidationError(MSG)'在視圖中被使用時,定製異常處理程序exceutes。如果在序列化程序中使用'raise ValidationError(msg)',那麼自定義異常處理程序不會**被使用。有沒有辦法以某種方式擴展DRF-JWT? – Rias