thelevicole / youtube-to-html5-loader

Load YouTube videos with the HTLML5 <video> element without needing iframes or the YouTube JS API.
https://thelevicole.com/youtube-to-html5-loader/
138 stars 25 forks source link

Add errror handling #19

Closed joanne-wu-hoo closed 2 years ago

joanne-wu-hoo commented 2 years ago

When we try to use the YoutubeToHtml5 loader to embed a private video (ie. https://www.youtube.com/watch?v=mYMZB39KiuQ), we get the following error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'streamingData')
    at YouTubeToHtml5.parseYoutubeMeta (YouTubeToHtml5.js?398e:1)

This PR addresses this error by:

  1. Validating references when accessing nested properties (ie. response.player_response && response.player_response.streamingData && response.player_response.streamingData.formats) and
  2. Throwing an error to be caught at a higher level

Related issue: https://github.com/thelevicole/youtube-to-html5-loader/issues/15