Przeglądaj źródła

Added error handling to YouTube functions, fixed issue importing playlists

Kristian Vos 4 lat temu
rodzic
commit
a16b348bec
2 zmienionych plików z 36 dodań i 4 usunięć
  1. 4 4
      backend/logic/actions/queueSongs.js
  2. 32 0
      backend/logic/utils.js

+ 4 - 4
backend/logic/actions/queueSongs.js

@@ -350,7 +350,7 @@ let lib = {
                             url,
                             musicOnly: false,
                         })
-                        .then((songIds) => next(null, songIds))
+                        .then((res) => next(null, res.songs))
                         .catch(next);
                 },
                 (songIds, next) => {
@@ -358,12 +358,12 @@ let lib = {
                     function checkDone() {
                         if (processed === songIds.length) next();
                     }
-                    for (let s = 0; s < songIds.length; s++) {
-                        lib.add(session, songIds[s], () => {
+                    songIds.forEach(songId => {
+                        lib.add(session, songId, () => {
                             processed++;
                             checkDone();
                         });
-                    }
+                    });
                 },
             ],
             async (err) => {

+ 32 - 0
backend/logic/utils.js

@@ -442,6 +442,18 @@ class UtilsModule extends CoreClass {
 
                             body = JSON.parse(body);
 
+                            if (body.error) {
+                                console.log(
+                                    "ERROR",
+                                    "GET_SONG_FROM_YOUTUBE",
+                                    `${body.error.message}`
+                                );
+                                return reject(new Error("An error has occured. Please try again later."));
+                            }
+
+                            if (body.items[0] === undefined)
+                                return reject(new Error("The specified video does not exist or cannot be publicly accessed."));
+
                             //TODO Clean up duration converter
                             let dur = body.items[0].contentDetails.duration;
                             dur = dur.replace("PT", "");
@@ -505,6 +517,15 @@ class UtilsModule extends CoreClass {
 
                         body = JSON.parse(body);
 
+                        if (body.error) {
+                            console.log(
+                                "ERROR",
+                                "FILTER_MUSIC_VIDEOS_YOUTUBE",
+                                `${body.error.message}`
+                            );
+                            return reject(new Error("An error has occured. Please try again later."));
+                        }
+
                         let songIds = [];
                         body.items.forEach((item) => {
                             const songId = item.id;
@@ -563,7 +584,18 @@ class UtilsModule extends CoreClass {
                         }
 
                         body = JSON.parse(body);
+
+                        if (body.error) {
+                            console.log(
+                                "ERROR",
+                                "GET_PLAYLIST_FROM_YOUTUBE",
+                                `${body.error.message}`
+                            );
+                            return reject(new Error("An error has occured. Please try again later."));
+                        }
+
                         songs = songs.concat(body.items);
+
                         if (body.nextPageToken)
                             getPage(body.nextPageToken, songs);
                         else {