error handling

master
Tomáš Mládek 2022-06-15 22:45:29 +02:00
parent b021ed90ec
commit f5ded75323
2 changed files with 14 additions and 0 deletions

View File

@ -27,6 +27,10 @@
const lastId = await result.text();
const currentResult = await fetch("grid.php");
if (!currentResult.ok) {
alert(currentResult.status);
return;
}
const current = await currentResult.json();
let choosing;
@ -65,15 +69,21 @@
async function random() {
const currentResult = await fetch("grid.php");
if (!currentResult.ok) {
alert(currentResult.status);
return;
}
const current = await currentResult.json();
const audios = current.filter((link) => link.audio);
if (!audios.length) {
alert(`Looks like there's no audios yet. Come back later!`);
return;
}
const videos = current.filter((link) => link.video);
if (!videos.length) {
alert(`Looks like there's no videos yet. Come back later!`);
return;
}
const videoId = videos[Math.floor(Math.random() * videos.length)].id;

View File

@ -33,6 +33,10 @@
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
const currentResult = await fetch("grid.php");
if (!currentResult.ok) {
alert(currentResult.status);
return;
}
const current = await currentResult.json();
videoUrl = current.find((link) => link.id == videoId).url;
audioUrl = current.find((link) => link.id == audioId).url;