Skip to content

Commit

Permalink
fix: raise a more specific error from subsidy http errors. ENT-7626
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Aug 30, 2023
1 parent dccac6f commit 3267289
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import ddt
from django.conf import settings
from requests.exceptions import HTTPError
from rest_framework import status
from rest_framework.reverse import reverse

Expand Down Expand Up @@ -1599,3 +1600,23 @@ def test_can_redeem_policy_no_price(self, mock_lms_client, mock_transactions_cac
assert response.json() == {
'detail': f'Could not determine price for content_key: {test_content_key}',
}

@mock.patch('enterprise_access.apps.subsidy_access_policy.subsidy_api.get_versioned_subsidy_client')
def test_can_redeem_subsidy_client_http_error(self, mock_get_client):
"""
Test that the can_redeem endpoint raises
an expected, specific exception when the subsidy REST API raises
an HTTPError.
"""
test_content_key = "course-v1:demox+1234+2T2023"
query_params = {'content_key': test_content_key}

mock_client = mock_get_client.return_value
mock_client.list_subsidy_transactions.side_effect = HTTPError

response = self.client.get(self.subsidy_access_policy_can_redeem_endpoint, query_params)

assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
assert response.json() == {
'detail': 'Subsidy Transaction API error: HTTPError occurred in Subsidy API request.',
}
11 changes: 10 additions & 1 deletion enterprise_access/apps/api/v1/views/subsidy_access_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,16 @@ def get_existing_redemptions(self, policies, lms_user_id):
for the given learner, filtered to only those transactions associated with **subsidies**
to which any of the given **policies** are associated.
"""
redemptions_map = get_redemptions_by_content_and_policy_for_learner(policies, lms_user_id)
try:
redemptions_map = get_redemptions_by_content_and_policy_for_learner(policies, lms_user_id)
except SubsidyAPIHTTPError as exc:
logger.exception(f'{exc} when fetching redemptions from subsidy API')
error_payload = exc.error_payload()
error_payload['detail'] = f"Subsidy Transaction API error: {error_payload['detail']}"
raise RedemptionRequestException(
detail=error_payload,
) from exc

for content_key, transactions_by_policy in redemptions_map.items():
for _, redemptions in transactions_by_policy.items():
for redemption in redemptions:
Expand Down
6 changes: 5 additions & 1 deletion enterprise_access/apps/subsidy_access_policy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ def error_response(self):
return self.__cause__.response # pylint: disable=no-member

def error_payload(self):
return self.error_response.json()
if self.error_response:
return self.error_response.json()
return {
'detail': str(self),
}
16 changes: 11 additions & 5 deletions enterprise_access/apps/subsidy_access_policy/subsidy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import logging
from collections import defaultdict

import requests

from .exceptions import SubsidyAPIHTTPError
from .utils import get_versioned_subsidy_client, request_cache, versioned_cache_key

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -34,11 +37,14 @@ def get_and_cache_transactions_for_learner(subsidy_uuid, lms_user_id):
return cached_response.value

client = get_versioned_subsidy_client()
response_payload = client.list_subsidy_transactions(
subsidy_uuid=subsidy_uuid,
lms_user_id=lms_user_id,
include_aggregates=False,
)
try:
response_payload = client.list_subsidy_transactions(
subsidy_uuid=subsidy_uuid,
lms_user_id=lms_user_id,
include_aggregates=False,
)
except requests.exceptions.HTTPError as exc:
raise SubsidyAPIHTTPError('HTTPError occurred in Subsidy API request.') from exc

result = {
'transactions': response_payload['results'],
Expand Down

0 comments on commit 3267289

Please sign in to comment.