Skip to content

Commit

Permalink
Fix validation error handling and improve response format
Browse files Browse the repository at this point in the history
  • Loading branch information
QuvonchbekBobojonov committed Dec 11, 2024
1 parent 1203073 commit dec1a83
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/success_response/success_rest_framework_simplejwt/views.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView, TokenViewBase
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request
from ..response import SuccessResponse


# Custom class for obtaining JWT access and refresh tokens, wrapping the response in SuccessResponse
class SuccessTokenObtainPairView(TokenObtainPairView):
class SuccessTokenViewBase(TokenViewBase):
"""Base class for customizing JWT token-related views with SuccessResponse."""

def handle_exception(self, serializer, request_data):
"""Handle exceptions during token processing and return a proper SuccessResponse."""
try:
serializer.is_valid(raise_exception=True)
except ValidationError as e:
detail = ' '.join(
f"{field}: {' '.join(errors)}" for field, errors in e.detail.items()
)
return SuccessResponse({'detail': detail}, success=False)
except Exception as e:
return SuccessResponse({'detail': str(e)}, success=False)

return SuccessResponse(serializer.data)

def post(self, request: Request, *args, **kwargs):
"""Process POST requests using the appropriate serializer."""
serializer = self.get_serializer(data=request.data)
return self.handle_exception(serializer, request.data)


# Custom class for obtaining JWT access and refresh tokens
class SuccessTokenObtainPairView(SuccessTokenViewBase, TokenObtainPairView):
"""Override TokenObtainPairView to wrap the response in SuccessResponse."""

def post(self, request, *args, **kwargs):
# Call the original post method from TokenObtainPairView to handle token generation
response = super().post(request, *args, **kwargs)
# Return the token data in the custom SuccessResponse format
return SuccessResponse(response.data)


# Custom class for refreshing JWT access tokens, wrapping the response in SuccessResponse
class SuccessTokenRefreshView(TokenRefreshView):
# Custom class for refreshing JWT access tokens
class SuccessTokenRefreshView(SuccessTokenViewBase, TokenRefreshView):
"""Override TokenRefreshView to wrap the response in SuccessResponse."""

def post(self, request, *args, **kwargs):
# Call the original post method from TokenRefreshView to handle token refresh
response = super().post(request, *args, **kwargs)
# Return the refreshed token data in the custom SuccessResponse format
return SuccessResponse(response.data)


# Custom class for verifying JWT tokens, wrapping the response in SuccessResponse
class SuccessTokenVerifyView(TokenVerifyView):
# Custom class for verifying JWT tokens
class SuccessTokenVerifyView(SuccessTokenViewBase, TokenVerifyView):
"""Override TokenVerifyView to wrap the response in SuccessResponse."""

def post(self, request, *args, **kwargs):
# Call the original post method from TokenVerifyView to verify the token
response = super().post(request, *args, **kwargs)
# Return the verification result in the custom SuccessResponse format
return SuccessResponse(response.data)

0 comments on commit dec1a83

Please sign in to comment.