From 65dd43ac6807d43f1af19975d4f0404a487cb71f Mon Sep 17 00:00:00 2001 From: Max Isom Date: Thu, 6 Jan 2022 16:25:59 -0600 Subject: [PATCH] Don't silently fail --- CHANGELOG.md | 2 + src/services/get-songs.ts | 86 +++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c5114a..5af4bb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Track lookups no longer fail silently (error is returned and logged) ## [0.3.1] - 2022-01-06 ### Fixed diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index ebf779a..55fb260 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -43,58 +43,50 @@ export default class { } async youtubeVideoSearch(query: string): Promise { - try { - const {items} = await this.ytsrQueue.add(async () => this.cache.wrap( - ytsr, - query, - { - limit: 10, - }, - { - expiresIn: ONE_HOUR_IN_SECONDS, - }, - )); + const {items} = await this.ytsrQueue.add(async () => this.cache.wrap( + ytsr, + query, + { + limit: 10, + }, + { + expiresIn: ONE_HOUR_IN_SECONDS, + }, + )); - let firstVideo: Video | undefined; + let firstVideo: Video | undefined; - for (const item of items) { - if (item.type === 'video') { - firstVideo = item; - break; - } + for (const item of items) { + if (item.type === 'video') { + firstVideo = item; + break; } - - if (!firstVideo) { - throw new Error('No video found.'); - } - - return await this.youtubeVideo(firstVideo.id); - } catch (_: unknown) { - return null; } + + if (!firstVideo) { + throw new Error('No video found.'); + } + + return this.youtubeVideo(firstVideo.id); } async youtubeVideo(url: string): Promise { - try { - const videoDetails = await this.cache.wrap( - this.youtube.videos.get, - cleanUrl(url), - { - expiresIn: ONE_HOUR_IN_SECONDS, - }, - ); + const videoDetails = await this.cache.wrap( + this.youtube.videos.get, + cleanUrl(url), + { + expiresIn: ONE_HOUR_IN_SECONDS, + }, + ); - return { - title: videoDetails.snippet.title, - artist: videoDetails.snippet.channelTitle, - length: toSeconds(parse(videoDetails.contentDetails.duration)), - url: videoDetails.id, - playlist: null, - isLive: videoDetails.snippet.liveBroadcastContent === 'live', - }; - } catch (_: unknown) { - return null; - } + return { + title: videoDetails.snippet.title, + artist: videoDetails.snippet.channelTitle, + length: toSeconds(parse(videoDetails.contentDetails.duration)), + url: videoDetails.id, + playlist: null, + isLive: videoDetails.snippet.liveBroadcastContent === 'live', + }; } async youtubePlaylist(listId: string): Promise { @@ -280,10 +272,6 @@ export default class { } private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, _: QueuedPlaylist | null): Promise { - try { - return await this.youtubeVideoSearch(`"${track.name}" "${track.artists[0].name}"`); - } catch (_: unknown) { - return null; - } + return this.youtubeVideoSearch(`"${track.name}" "${track.artists[0].name}"`); } }