Skip to content

Commit

Permalink
refactor: Relocate convert_exceptions, remove utils
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Aug 30, 2024
1 parent e376a76 commit ca8a071
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)

from openedx.core.djangoapps.content_libraries import api, permissions
from openedx.core.djangoapps.content_libraries.utils import convert_exceptions
from openedx.core.djangoapps.content_libraries.views import convert_exceptions
from openedx.core.djangoapps.content_libraries.serializers import (
ContentLibraryCollectionSerializer,
ContentLibraryCollectionCreateOrUpdateSerializer,
Expand Down
44 changes: 0 additions & 44 deletions openedx/core/djangoapps/content_libraries/utils.py

This file was deleted.

34 changes: 33 additions & 1 deletion openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
https://github.com/openedx/edx-platform/pull/30456
"""

from functools import wraps
import itertools
import json
import logging
Expand All @@ -83,6 +84,7 @@
from pylti1p3.exception import LtiException, OIDCException

import edx_api_doc_tools as apidocs
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locator import LibraryLocatorV2, LibraryUsageLocatorV2
from organizations.api import ensure_organization
from organizations.exceptions import InvalidOrganizationException
Expand Down Expand Up @@ -119,13 +121,43 @@
from openedx.core.djangoapps.xblock import api as xblock_api

from .models import ContentLibrary, LtiGradedResource, LtiProfile
from .utils import convert_exceptions


User = get_user_model()
log = logging.getLogger(__name__)


def convert_exceptions(fn):
"""
Catch any Content Library API exceptions that occur and convert them to
DRF exceptions so DRF will return an appropriate HTTP response
"""

@wraps(fn)
def wrapped_fn(*args, **kwargs):
try:
return fn(*args, **kwargs)
except InvalidKeyError as exc:
log.exception(str(exc))
raise NotFound # lint-amnesty, pylint: disable=raise-missing-from
except api.ContentLibraryNotFound:
log.exception("Content library not found")
raise NotFound # lint-amnesty, pylint: disable=raise-missing-from
except api.ContentLibraryBlockNotFound:
log.exception("XBlock not found in content library")
raise NotFound # lint-amnesty, pylint: disable=raise-missing-from
except api.LibraryBlockAlreadyExists as exc:
log.exception(str(exc))
raise ValidationError(str(exc)) # lint-amnesty, pylint: disable=raise-missing-from
except api.InvalidNameError as exc:
log.exception(str(exc))
raise ValidationError(str(exc)) # lint-amnesty, pylint: disable=raise-missing-from
except api.BlockLimitReachedError as exc:
log.exception(str(exc))
raise ValidationError(str(exc)) # lint-amnesty, pylint: disable=raise-missing-from
return wrapped_fn


class LibraryApiPaginationDocs:
"""
API docs for query params related to paginating ContentLibraryMetadata objects.
Expand Down

0 comments on commit ca8a071

Please sign in to comment.