0
DRFを使用する場合、DjangoのValueError(django.core.exceptions)とIntegrityError(django.db)は処理されません。DRf、未処理例外を処理します
DRFのdef exception_handler
がために例外処理コードを持っている(にApiException、Http404、PermissionDenied)
は、だから私は
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)
if isinstance(exc, ValidationError) or isinstance(exc, IntegrityError):
data = {
'errors': str(exc)
}
set_rollback() # not sure about this line
response = Response(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return response
としての私のカスタム例外ハンドラを作成することができます
Http404
elif isinstance(exc, Http404):
msg = _('Not found.')
data = {'detail': six.text_type(msg)}
set_rollback()
return Response(data, status=status.HTTP_404_NOT_FOUND)
のコードです
私はコードのset_rollback()
行の目的についてはわかりませんが、私はこのタラで安全だとは分かりませんe。