From 611ad91088de50e10306bf515e45f877209a85d1 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Wed, 20 Oct 2021 18:40:59 +0300 Subject: [PATCH] better frontend error handling --- public/index.css | 14 ++++++++++++++ public/index.html | 1 + public/index.js | 48 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/public/index.css b/public/index.css index 0eda3e3..c48dfaa 100644 --- a/public/index.css +++ b/public/index.css @@ -418,4 +418,18 @@ input:checked + .slider:before { .album-downloading { border-radius: 10px; +} + +.error { + background-color: rgb(255, 155, 155, 0.3); + padding: 20px; + border-radius: 15px; + border: 3px solid rgb(255, 155, 155, 0.8); + text-align: center; + margin: 15px; + width: 400px; + display: none; /* this is changed by the js */ +} +.error .big { + font-size: x-large; } \ No newline at end of file diff --git a/public/index.html b/public/index.html index d4c2a0b..0f37552 100644 --- a/public/index.html +++ b/public/index.html @@ -22,6 +22,7 @@ +
diff --git a/public/index.js b/public/index.js index 14d6bb5..b8ab3ff 100644 --- a/public/index.js +++ b/public/index.js @@ -76,17 +76,36 @@ function startDownload(id, isAlbum) { } else if (d.key === 'download') { download(d.data); } else if (d.key === 'finishDownload') { - //change(); - return; + log = addlog(log, 'Download finished'); } document.getElementById('progress-album').innerHTML = `
${title || ''}
by ${artist || ''}
${log || ''}
`; } + ws.onerror = (e) => { + console.log('error: ' + e); + error(e.toString()); + } + ws.onclose = (e) => { + change(); + if (e.code !== 1000) error(`websocket closed unexpectedly with code ${e.code}\n${e.reason}`); + } +} + +function error(e) { + document.getElementById('error').innerHTML = `
error!
${e.split('\n').join('
')}`; + document.getElementById('error').style.display = 'block'; + console.error(e); +} +function clearError() { + document.getElementById('error').innerHTML = ''; + document.getElementById('error').style.display = 'none'; } let change; // fuck off js window.onload = () => { + clearError(); + // dirty theme hacks :tm: const color = window.getComputedStyle(document.querySelector('body')).getPropertyValue('color'); @@ -120,12 +139,20 @@ window.onload = () => { search.setAttribute('placeholder', placeholders[Math.floor(Math.random() * placeholders.length)]); change = async () => { + clearError(); const value = document.getElementById('album-search').value; if (value === '') return document.getElementById('albums').innerHTML = ''; document.getElementById('progress-album').innerHTML = ''; document.getElementById('progress-bar-wrapper').innerHTML = ''; document.getElementById('albums').innerHTML = '
'; - const d = await axios.get('/api/search', {params: {search: value}}); + let d; + try { + d = await axios.get('/api/search', {params: {search: value}}); + } catch(err) { + error(err.toString()); + document.getElementById('albums').innerHTML = ''; + return; + } document.getElementById('albums').innerHTML = d.data.map(d => `
${d.title}
by ${d.artist.name}
` ).join('
'); @@ -135,21 +162,30 @@ window.onload = () => { for (c of document.getElementById('albums').children) { if (c.children[5]) { let id = c.id.split('-')[1]; - c.children[5].onclick = (a) => { + c.children[5].onclick = () => { + clearError(); startDownload(id, true); } } let id = c.id.split('-')[1]; if (document.getElementById('album-bottom-' + id)) { document.getElementById('album-bottom-' + id).innerHTML = '
'; - const album = await axios.get('/api/album', {params: {id: id}}); + let album; + try { + album = await axios.get('/api/album', {params: {id: id}}); + } catch(err) { + error(err.toString()); + document.getElementById('album-bottom-' + id).innerHTML = ''; + return; + } + document.getElementById('album-bottom-' + id).innerHTML = album.data.tracks.map(d => `
${d.artist} - ${d.title} ${formatTime(d.duration)}
` ).join(''); for (track of document.getElementById('album-bottom-' + id).children) { let trackId = track.id.split('-')[1]; track.children[1].children[0].onclick = () => { - console.log(trackId); + clearError(); startDownload(trackId, false); } }