diff --git a/lms/djangoapps/commerce/http.py b/lms/djangoapps/commerce/http.py index 443703a51f..ba9e067c0a 100644 --- a/lms/djangoapps/commerce/http.py +++ b/lms/djangoapps/commerce/http.py @@ -1,6 +1,6 @@ """ HTTP-related entities. """ -from rest_framework.status import HTTP_503_SERVICE_UNAVAILABLE, HTTP_200_OK +from rest_framework.status import HTTP_500_INTERNAL_SERVER_ERROR, HTTP_200_OK from util.json_request import JsonResponse @@ -13,9 +13,12 @@ class DetailResponse(JsonResponse): super(DetailResponse, self).__init__(object=data, status=status) -class ApiErrorResponse(DetailResponse): - """ Response returned when calls to the E-Commerce API fail or the returned data is invalid. """ +class InternalRequestErrorResponse(DetailResponse): + """ Response returned when an internal service request fails. """ - def __init__(self): - message = 'Call to E-Commerce API failed. Order creation failed.' - super(ApiErrorResponse, self).__init__(message=message, status=HTTP_503_SERVICE_UNAVAILABLE) + def __init__(self, internal_message): + message = ( + 'Call to E-Commerce API failed. Internal Service Message: [{internal_message}]' + .format(internal_message=internal_message) + ) + super(InternalRequestErrorResponse, self).__init__(message=message, status=HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/lms/djangoapps/commerce/tests/test_views.py b/lms/djangoapps/commerce/tests/test_views.py index 434178bea8..3b525689cd 100644 --- a/lms/djangoapps/commerce/tests/test_views.py +++ b/lms/djangoapps/commerce/tests/test_views.py @@ -48,10 +48,11 @@ class OrdersViewTests(EnrollmentEventTestMixin, EcommerceApiTestMixin, ModuleSto actual = json.loads(response.content)['detail'] self.assertEqual(actual, expected_msg) - def assertValidEcommerceApiErrorResponse(self, response): + def assertValidEcommerceInternalRequestErrorResponse(self, response): """ Asserts the response is a valid response sent when the E-Commerce API is unavailable. """ - self.assertEqual(response.status_code, 503) - self.assertResponseMessage(response, 'Call to E-Commerce API failed. Order creation failed.') + self.assertEqual(response.status_code, 500) + actual = json.loads(response.content)['detail'] + self.assertIn('Call to E-Commerce API failed', actual) def assertUserNotEnrolled(self): """ Asserts that the user is NOT enrolled in the course, and that an enrollment event was NOT fired. """ @@ -112,7 +113,7 @@ class OrdersViewTests(EnrollmentEventTestMixin, EcommerceApiTestMixin, ModuleSto with self.mock_create_order(side_effect=TimeoutError): response = self._post_to_view() - self.assertValidEcommerceApiErrorResponse(response) + self.assertValidEcommerceInternalRequestErrorResponse(response) self.assertUserNotEnrolled() def test_ecommerce_api_error(self): @@ -122,7 +123,7 @@ class OrdersViewTests(EnrollmentEventTestMixin, EcommerceApiTestMixin, ModuleSto with self.mock_create_order(side_effect=ApiError): response = self._post_to_view() - self.assertValidEcommerceApiErrorResponse(response) + self.assertValidEcommerceInternalRequestErrorResponse(response) self.assertUserNotEnrolled() def _test_successful_ecommerce_api_call(self): diff --git a/lms/djangoapps/commerce/views.py b/lms/djangoapps/commerce/views.py index 8e46d4a6e5..46f0b92574 100644 --- a/lms/djangoapps/commerce/views.py +++ b/lms/djangoapps/commerce/views.py @@ -10,7 +10,7 @@ from rest_framework.views import APIView from commerce.api import EcommerceAPI from commerce.constants import OrderStatus, Messages from commerce.exceptions import ApiError, InvalidConfigurationError -from commerce.http import DetailResponse, ApiErrorResponse +from commerce.http import DetailResponse, InternalRequestErrorResponse from course_modes.models import CourseMode from courseware import courses from enrollment.api import add_enrollment @@ -120,6 +120,6 @@ class OrdersView(APIView): msg = Messages.ORDER_INCOMPLETE_ENROLLED.format(order_number=order_number) return DetailResponse(msg, status=HTTP_202_ACCEPTED) - except ApiError: + except ApiError as err: # The API will handle logging of the error. - return ApiErrorResponse() + return InternalRequestErrorResponse(err.message)