forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use convert_exceptions + update tests
- Loading branch information
1 parent
cf9e906
commit 5bf9422
Showing
4 changed files
with
94 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" Utils used for the content libraries. """ | ||
|
||
from functools import wraps | ||
import logging | ||
|
||
from rest_framework.exceptions import NotFound, ValidationError | ||
|
||
from opaque_keys import InvalidKeyError | ||
|
||
from openedx.core.djangoapps.content_libraries import api | ||
|
||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters