Skip to content

Commit

Permalink
Merge pull request #519 from edx/iahmad/ENT-1949-Improved-logging-for…
Browse files Browse the repository at this point in the history
…-enterprise-api

ENT-1949 Additional logging for enterprise api
  • Loading branch information
irfanuddinahmad authored Jul 11, 2019
2 parents 4b552b6 + e56b1ed commit 3df0a15
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Change Log
Unreleased
----------

[1.6.21] - 2019-07-11
---------------------

* Added additional logging for enterprise api

[1.6.20] - 2019-07-10
---------------------

Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = "1.6.20"
__version__ = "1.6.21"

default_app_config = "enterprise.apps.EnterpriseConfig" # pylint: disable=invalid-name
36 changes: 36 additions & 0 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import absolute_import, unicode_literals

import copy
from logging import getLogger

from edx_rest_api_client.exceptions import HttpClientError
from rest_framework import serializers
Expand All @@ -19,6 +20,8 @@
from enterprise.constants import ENTERPRISE_PERMISSION_GROUPS
from enterprise.utils import CourseEnrollmentDowngradeError, CourseEnrollmentPermissionError, track_enrollment

LOGGER = getLogger(__name__)


class ImmutableStateSerializer(serializers.Serializer):
"""
Expand Down Expand Up @@ -159,11 +162,16 @@ def validate_username(self, value):
try:
user = User.objects.get(username=value)
except User.DoesNotExist:
error_message = ('[Enterprise API] The username for creating an EnterpriseCourseEnrollment'
' record does not exist. User: {}').format(value)
LOGGER.error(error_message)
raise serializers.ValidationError("User does not exist")

try:
enterprise_customer_user = models.EnterpriseCustomerUser.objects.get(user_id=user.pk)
except models.EnterpriseCustomerUser.DoesNotExist:
error_message = '[Enterprise API] User has no EnterpriseCustomerUser. User: {}'.format(value)
LOGGER.error(error_message)
raise serializers.ValidationError("User has no EnterpriseCustomerUser")

self.enterprise_customer_user = enterprise_customer_user
Expand Down Expand Up @@ -312,6 +320,9 @@ def validate_username(self, value):
try:
self.user = User.objects.get(username=value)
except User.DoesNotExist:
error_message = ('[Enterprise API] Saving to EnterpriseCustomerUser failed'
' due to non-existing user. User: {}').format(value)
LOGGER.error(error_message)
raise serializers.ValidationError("User does not exist")

return value
Expand Down Expand Up @@ -562,6 +573,20 @@ def create(self, validated_data):
else:
enterprise_customer_user.unenroll(course_run_id)
except (CourseEnrollmentDowngradeError, CourseEnrollmentPermissionError, HttpClientError) as exc:
error_message = (
'[Enterprise API] An exception occurred while enrolling the user.'
' EnterpriseCustomer: {enterprise_customer}, LmsUser: {lms_user}, TpaUser: {tpa_user},'
' UserEmail: {user_email}, CourseRun: {course_run_id}, CourseMode {course_mode}, Message: {exc}.'
).format(
enterprise_customer=enterprise_customer,
lms_user=lms_user,
tpa_user=tpa_user,
user_email=user_email,
course_run_id=course_run_id,
course_mode=course_mode,
exc=str(exc)
)
LOGGER.error(error_message)
validated_data['detail'] = str(exc)
return validated_data

Expand Down Expand Up @@ -655,6 +680,11 @@ def validate_course_run_id(self, value):
enterprise_customer = self.context.get('enterprise_customer')

if not enterprise_customer.catalog_contains_course(value):
error_message = ('[Enterprise API] The course run id is not in the catalog for the Enterprise Customer.'
' EnterpriseCustomer: {enterprise_customer}, CourseRun: {course_run_id}').format(
course_run_id=value,
enterprise_customer=enterprise_customer.name)
LOGGER.error(error_message)
raise serializers.ValidationError(
'The course run id {course_run_id} is not in the catalog '
'for Enterprise Customer {enterprise_customer}'.format(
Expand All @@ -673,6 +703,12 @@ def validate(self, data): # pylint: disable=arguments-differ
tpa_user_id = data.get('tpa_user_id')
user_email = data.get('user_email')
if not lms_user_id and not tpa_user_id and not user_email:
error_message = ('[Enterprise API] ID missing for mapping to an EnterpriseCustomerUser.'
' LmsUser: {lms_user_id}, TpaUser: {tpa_user_id}, UserEmail: {user_email}').format(
lms_user_id=lms_user_id,
tpa_user_id=tpa_user_id,
user_email=user_email)
LOGGER.error(error_message)
raise serializers.ValidationError(
'At least one of the following fields must be specified and map to an EnterpriseCustomerUser: '
'lms_user_id, tpa_user_id, user_email'
Expand Down
25 changes: 23 additions & 2 deletions enterprise/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ def course_detail(self, request, pk, course_key): # pylint: disable=invalid-nam
enterprise_customer_catalog = self.get_object()
course = enterprise_customer_catalog.get_course(course_key)
if not course:
error_message = _(
'[Enterprise API] CourseKey not found in the Catalog. Course: {course_key}, Catalog: {catalog_id}'
).format(
course_key=course_key,
catalog_id=enterprise_customer_catalog.uuid,
)
LOGGER.warning(error_message)
raise Http404

context = self.get_serializer_context()
Expand All @@ -390,6 +397,13 @@ def course_run_detail(self, request, pk, course_id): # pylint: disable=invalid-
enterprise_customer_catalog = self.get_object()
course_run = enterprise_customer_catalog.get_course_run(course_id)
if not course_run:
error_message = _(
'[Enterprise API] CourseRun not found in the Catalog. CourseRun: {course_id}, Catalog: {catalog_id}'
).format(
course_id=course_id,
catalog_id=enterprise_customer_catalog.uuid,
)
LOGGER.warning(error_message)
raise Http404

context = self.get_serializer_context()
Expand All @@ -411,6 +425,13 @@ def program_detail(self, request, pk, program_uuid): # pylint: disable=invalid-
enterprise_customer_catalog = self.get_object()
program = enterprise_customer_catalog.get_program(program_uuid)
if not program:
error_message = _(
'[Enterprise API] Program not found in the Catalog. Program: {program_uuid}, Catalog: {catalog_id}'
).format(
program_uuid=program_uuid,
catalog_id=enterprise_customer_catalog.uuid,
)
LOGGER.warning(error_message)
raise Http404

context = self.get_serializer_context()
Expand Down Expand Up @@ -532,8 +553,8 @@ def post(self, request):
return Response(data, status=HTTP_200_OK)
except SMTPException:
error_message = _(
'[Enterprise API] Failure in sending e-mail to {token_cs_email} for {token_email}'
' from {token_enterprise_name}'
'[Enterprise API] Failure in sending e-mail to support.'
' SupportEmail: {token_cs_email}, UserEmail: {token_email}, EnterpriseName: {token_enterprise_name}'
).format(
token_cs_email=cs_email,
token_email=email,
Expand Down

0 comments on commit 3df0a15

Please sign in to comment.