From 7481a29c4f49429b6646995cb8a9dea418160c39 Mon Sep 17 00:00:00 2001 From: Sam12354 <161862599+Sam12354@users.noreply.github.com> Date: Fri, 10 Apr 2026 20:57:13 +0300 Subject: [PATCH] Add error handling to getMoreSongs --- lyrics-search/script.js | 49 ++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/lyrics-search/script.js b/lyrics-search/script.js index c150086a..e51a6bc0 100644 --- a/lyrics-search/script.js +++ b/lyrics-search/script.js @@ -18,27 +18,25 @@ function showData(data) { result.innerHTML = ` `; if (data.prev || data.next) { more.innerHTML = ` - ${ - data.prev - ? `` - : '' + ${data.prev + ? `` + : '' } - ${ - data.next - ? `` - : '' + ${data.next + ? `` + : '' } `; } else { @@ -48,10 +46,21 @@ function showData(data) { // Get prev and next songs async function getMoreSongs(url) { - const res = await fetch(`https://cors-anywhere.herokuapp.com/${url}`); - const data = await res.json(); - showData(data); + try { + const res = await fetch(`https://cors-anywhere.herokuapp.com/${url}`); + + if (!res.ok) { + throw new Error('Failed to fetch songs'); + } + + const data = await res.json(); + showData(data); + + } catch (error) { + console.log('Something went wrong', error); + } + } // Get lyrics for song @@ -59,12 +68,12 @@ async function getLyrics(artist, songTitle) { const res = await fetch(`${apiURL}/v1/${artist}/${songTitle}`); const data = await res.json(); - if (data.error) { - result.innerHTML = data.error; - } else { - const lyrics = data.lyrics.replace(/(\r\n|\r|\n)/g, '
'); + if (data.error) { + result.innerHTML = data.error; + } else { + const lyrics = data.lyrics.replace(/(\r\n|\r|\n)/g, '
'); - result.innerHTML = ` + result.innerHTML = `

${artist} - ${songTitle}

${lyrics} `;