From 1c656274f410c01c62823a7a97cecc9ce2e9cee3 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Waheed Date: Mon, 25 Nov 2024 11:28:28 +0500 Subject: [PATCH] fix: handled exception gracefully and returned proper response --- .../apps/frontend_app_ecommerce/views.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/commerce_coordinator/apps/frontend_app_ecommerce/views.py b/commerce_coordinator/apps/frontend_app_ecommerce/views.py index bd818c6f..29aa5fa3 100644 --- a/commerce_coordinator/apps/frontend_app_ecommerce/views.py +++ b/commerce_coordinator/apps/frontend_app_ecommerce/views.py @@ -10,7 +10,7 @@ from edx_rest_framework_extensions.permissions import LoginRedirectIfUnauthenticated from rest_framework.exceptions import PermissionDenied from rest_framework.response import Response -from rest_framework.status import HTTP_303_SEE_OTHER +from rest_framework.status import HTTP_303_SEE_OTHER, HTTP_400_BAD_REQUEST from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView @@ -94,18 +94,21 @@ def get(self, request): if not request.user.lms_user_id: # pragma: no cover raise PermissionDenied(detail="Could not detect LMS user id.") - order_data = OrderHistoryRequested.run_filter(request, params) + try: + order_data = OrderHistoryRequested.run_filter(request, params) - output_orders = [] + output_orders = [] - for order_set in order_data: - output_orders.extend(order_set['results']) + for order_set in order_data: + output_orders.extend(order_set['results']) - output = { - "count": request.query_params['page_size'], # This suppresses the ecomm mfe Order History Pagination ctrl - "next": None, - "previous": None, - "results": sorted(output_orders, key=lambda item: date_conv(item["date_placed"]), reverse=True) - } + output = { + "count": request.query_params['page_size'], # This suppresses the ecomm mfe Order History Pagination ctrl + "next": None, + "previous": None, + "results": sorted(output_orders, key=lambda item: date_conv(item["date_placed"]), reverse=True) + } - return Response(output) + return Response(output) + except Exception as exc: + return Response(status=HTTP_400_BAD_REQUEST, data=str(exc))