Fix case, where pakubiiti recieved only 2 albums from the server

Use a while loop instead of the previous for loop
Cap the amount of retries to not create infinite loops
Show an error message on the client when this cap is reached without
getting the required amount of albums
This commit is contained in:
Mihkel Martin Kasterpalu 2025-02-10 12:42:40 +02:00
parent e08b0e87fb
commit a3687ac56b
3 changed files with 32 additions and 7 deletions

View file

@ -1,15 +1,19 @@
import { albumState } from '$lib/server/AlbumState.svelte'; import { albumState } from '$lib/server/AlbumState.svelte';
import { spotifyAPI } from '$lib/server/Spotify.svelte'; import { spotifyAPI } from '$lib/server/Spotify.svelte';
import type { AlbumSolveState } from '$lib/types'; import type { AlbumSolveState } from '$lib/types';
import { json } from '@sveltejs/kit'; import { error, json } from '@sveltejs/kit';
const maxTries = 10;
export async function GET({ params }) { export async function GET({ params }) {
const count = params.count || 1; const count = Number(params.count) || 1;
const albums: AlbumSolveState[] = []; const albums: AlbumSolveState[] = [];
let tries = 0;
for (let i = 0; i < count; i++) { while (albums.length < count && tries++ < maxTries) {
const album = await spotifyAPI.getRandomAlbum(); const album = await spotifyAPI.getRandomAlbum();
if (album) { if (album) {
const image = album.images.at(0); const image = album.images.at(0);
if (!image?.url) { if (!image?.url) {
@ -24,6 +28,12 @@ export async function GET({ params }) {
} }
} }
if (albums.length !== count) {
albumState.setAlbums([]);
return error(500, "Couldn't get albums from Spotify.");
}
albumState.setAlbums(albums); albumState.setAlbums(albums);
return json({ albums: albums }); return json({ albums: albums });

View file

@ -56,6 +56,11 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
images: shuffleArray(albumImages), images: shuffleArray(albumImages),
artists: shuffleArray(albumArtists) artists: shuffleArray(albumArtists)
}; };
})
.catch((err) => {
console.log(err);
return undefined;
}); });
return { return {

View file

@ -112,10 +112,20 @@
</div> </div>
{@render footer(true)} {@render footer(true)}
{:then albums} {:then albums}
{#if albums}
<div class="grid w-full gap-4" in:fade={{ duration: 150, delay: 150, easing: expoOut }}> <div class="grid w-full gap-4" in:fade={{ duration: 150, delay: 150, easing: expoOut }}>
{@render playArea(albums as AlbumData)} {@render playArea(albums as AlbumData)}
</div> </div>
{@render footer(false)} {@render footer(false)}
{:else}
<p class="mx-auto mt-16 max-w-prose text-center text-lg text-red-500">
<strong>Serveris tekkis mingi error.</strong>
<br />No clue miks see katki on, sorry.
</p>
<p class="mx-auto mt-6 max-w-prose text-center text-lg text-red-500">
Proovi uuesti või kirjuta mulle.
</p>
{/if}
{/await} {/await}
{:else} {:else}
<div class="grid w-full gap-4" out:fade={{ duration: 150, easing: expoOut }}> <div class="grid w-full gap-4" out:fade={{ duration: 150, easing: expoOut }}>