Skip to content

Commit

Permalink
Merge pull request #1371 from appsembler/bugfix/ENG-97-catch-and-retr…
Browse files Browse the repository at this point in the history
…y-yt-api-errs

ENG-97 Refactor try/catch for window.onYouTubeIframeAPIReady.resolve on video initialization
  • Loading branch information
bryanlandia committed Sep 18, 2023
2 parents 6eaa2e3 + 46a894d commit 460a6fa
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions common/lib/xmodule/xmodule/js/src/video/01_initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

};
Expand Down

0 comments on commit 460a6fa

Please sign in to comment.