From 46a894d25b1b22c574f5126692eeffc98c90ecf6 Mon Sep 17 00:00:00 2001 From: Bryan Wilson Date: Thu, 14 Sep 2023 15:37:35 -0700 Subject: [PATCH] Refactor try/catch on window.onYouTubeIframeAPIReady.resolve Rename Error instance as seems to have caused a name collision when minified --- .../xmodule/js/src/video/01_initialize.js | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/common/lib/xmodule/xmodule/js/src/video/01_initialize.js b/common/lib/xmodule/xmodule/js/src/video/01_initialize.js index c43ab4b1693..f386c5bb1ed 100644 --- a/common/lib/xmodule/xmodule/js/src/video/01_initialize.js +++ b/common/lib/xmodule/xmodule/js/src/video/01_initialize.js @@ -174,11 +174,27 @@ function(VideoPlayer, i18n, moment, _) { throw new Error('Too many OnYouTubeIframeAPIReady retries after TypeError...giving up.'); } + handleYTAPIErr = function(ytapierr) { + console.error('Error while trying to resolve the Deferred object responsible for calling OnYouTubeIframeAPIReady callbacks.'); + console.debug('window.onYouTubeIframeAPIReady is ' + window.onYouTubeIframeAPIReady); + console.error(ytapierr); + if (ytapierr instanceof TypeError) { // expecting TypeError: window.onYouTubeIframeAPIReady.resolve is not a function + setTimeout(setupOnYouTubeIframeAPIReady, 500); // Try again up to defined max calls. + } + else { + throw ytapierr; + } + }; + try { _oldOnYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady || undefined; window.onYouTubeIframeAPIReady = function() { - window.onYouTubeIframeAPIReady.resolve(); + try { // this additional inner try/catch shouldn't really be needed but it's here just in case. + window.onYouTubeIframeAPIReady.resolve(); + } catch (ytapiresolveerr) { + handleYTAPIErr(ytapiresolveerr); + } }; window.onYouTubeIframeAPIReady.resolve = _youtubeApiDeferred.resolve; @@ -187,16 +203,8 @@ function(VideoPlayer, i18n, moment, _) { if (_oldOnYouTubeIframeAPIReady) { window.onYouTubeIframeAPIReady.done(_oldOnYouTubeIframeAPIReady); } - } catch (e) { - console.error('Error while trying to resolve the Deferred object responsible for calling OnYouTubeIframeAPIReady callbacks.'); - console.error('window.onYouTubeIframeAPIReady is ' + window.onYouTubeIframeAPIReady); - console.error(e); - if (e instanceof TypeError) { - setTimeout(setupOnYouTubeIframeAPIReady, 500); // Try again up to defined max calls. - } - else { - throw e; - } + } catch (ytapierr) { + handleYTAPIErr(ytapierr); } };