From bfbeeb1d848d875c3629508e3ef484453bf5d1d1 Mon Sep 17 00:00:00 2001 From: irfanuddinahmad Date: Wed, 18 Apr 2018 12:55:28 +0500 Subject: [PATCH] added exception decorator --- .../tests/test_transcripts_utils.py | 40 +++++++++++++++++++ .../xmodule/video_module/transcripts_utils.py | 24 ++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/tests/test_transcripts_utils.py b/cms/djangoapps/contentstore/tests/test_transcripts_utils.py index bc4c1b6777..2a004433f8 100644 --- a/cms/djangoapps/contentstore/tests/test_transcripts_utils.py +++ b/cms/djangoapps/contentstore/tests/test_transcripts_utils.py @@ -959,3 +959,43 @@ class TestGetTranscript(SharedModuleStoreTestCase): exception_message = text_type(no_en_transcript_exception.exception) self.assertEqual(exception_message, 'No transcript for `en` language') + + @ddt.data( + transcripts_utils.TranscriptsGenerationException, + UnicodeDecodeError('aliencodec', b'\x02\x01', 1, 2, 'alien codec found!') + ) + @patch('xmodule.video_module.transcripts_utils.Transcript') + def test_get_transcript_val_exceptions(self, exception_to_raise, mock_Transcript): + """ + Verify that `get_transcript_from_val` function raises `NotFoundError` when specified exceptions raised. + """ + mock_Transcript.convert.side_effect = exception_to_raise + transcripts_info = self.video.get_transcripts_info() + lang = self.video.get_default_transcript_language(transcripts_info) + edx_video_id = transcripts_utils.clean_video_id(self.video.edx_video_id) + with self.assertRaises(NotFoundError): + transcripts_utils.get_transcript_from_val( + edx_video_id, + lang=lang, + output_format=transcripts_utils.Transcript.SRT + ) + + @ddt.data( + transcripts_utils.TranscriptsGenerationException, + UnicodeDecodeError('aliencodec', b'\x02\x01', 1, 2, 'alien codec found!') + ) + @patch('xmodule.video_module.transcripts_utils.Transcript') + def test_get_transcript_content_store_exceptions(self, exception_to_raise, mock_Transcript): + """ + Verify that `get_transcript_from_contentstore` function raises `NotFoundError` when specified exceptions raised. + """ + mock_Transcript.asset.side_effect = exception_to_raise + transcripts_info = self.video.get_transcripts_info() + lang = self.video.get_default_transcript_language(transcripts_info) + with self.assertRaises(NotFoundError): + transcripts_utils.get_transcript_from_contentstore( + self.video, + language=lang, + output_format=transcripts_utils.Transcript.SRT, + transcripts_info=transcripts_info + ) diff --git a/common/lib/xmodule/xmodule/video_module/transcripts_utils.py b/common/lib/xmodule/xmodule/video_module/transcripts_utils.py index 58f7b54a27..47cc06faf8 100644 --- a/common/lib/xmodule/xmodule/video_module/transcripts_utils.py +++ b/common/lib/xmodule/xmodule/video_module/transcripts_utils.py @@ -2,6 +2,7 @@ Utility functions for transcripts. ++++++++++++++++++++++++++++++++++ """ +from functools import wraps from django.conf import settings import os import copy @@ -49,6 +50,26 @@ class TranscriptsRequestValidationException(Exception): # pylint: disable=missi pass +def exception_decorator(func): + """ + Generate NotFoundError for TranscriptsGenerationException, UnicodeDecodeError. + + Args: + `func`: Input function + + Returns: + 'wrapper': Decorated function + """ + @wraps(func) + def wrapper(*args, **kwds): + try: + return func(*args, **kwds) + except (TranscriptsGenerationException, UnicodeDecodeError) as ex: + log.exception(text_type(ex)) + raise NotFoundError + return wrapper + + def generate_subs(speed, source_speed, source_subs): """ Generate transcripts from one speed to another speed. @@ -855,6 +876,7 @@ class VideoTranscriptsMixin(object): } +@exception_decorator def get_transcript_from_val(edx_video_id, lang=None, output_format=Transcript.SRT): """ Get video transcript from edx-val. @@ -909,6 +931,7 @@ def get_transcript_for_video(video_location, subs_id, file_name, language): return input_format, base_name, content +@exception_decorator def get_transcript_from_contentstore(video, language, output_format, transcripts_info, youtube_id=None): """ Get video transcript from content store. @@ -954,7 +977,6 @@ def get_transcript_from_contentstore(video, language, output_format, transcripts language_prefix = '{}_'.format(language) if language else '' transcript_name = u'{}{}.{}'.format(language_prefix, base_name, output_format) transcript_content = Transcript.convert(transcript_content, input_format=input_format, output_format=output_format) - if not transcript_content.strip(): raise NotFoundError('No transcript content')