From 9f098e5a83ff0a6fd47c277a86769c9aae41d023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20P=C4=99dzich?= Date: Mon, 9 Jan 2023 23:42:03 +0100 Subject: [PATCH] Refactor error message generation --- src/utils/getPlaylistLayoutProps.ts | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/utils/getPlaylistLayoutProps.ts b/src/utils/getPlaylistLayoutProps.ts index 3c38e54..a844e87 100644 --- a/src/utils/getPlaylistLayoutProps.ts +++ b/src/utils/getPlaylistLayoutProps.ts @@ -15,14 +15,7 @@ export async function getPlaylistLayoutProps(Astro: Readonly) { `https://raw.githubusercontent.com/mackorone/spotify-playlist-archive/main/playlists/pretty/${playlistId}.json` ); - if (!githubResponse.ok) { - const errorMessage = - githubResponse.status === 404 - ? "Astro playlist doesn't exist" - : "Failed to fetch playlist's data"; - - throw new Error(errorMessage); - } + if (!githubResponse.ok) throw new Error(githubResponse.status.toString()); playlist = (await githubResponse.json()) as PlaylistSnapshot; description = decode(playlist.description, { level: 'html5' }); @@ -31,21 +24,16 @@ export async function getPlaylistLayoutProps(Astro: Readonly) { playlist.unique_name !== playlist.original_name ? `${playlist.unique_name} (${playlist.original_name})` : playlist.original_name; - } catch (e) { - const expectedErrorMessages = [ - "Failed to fetch playlist's data", - "Astro playlist doesn't exist" - ]; - const [miscError] = expectedErrorMessages; - const errorMessage = (e as Error).message; + } catch (error) { + const isNotFoundError = (error as Error).message === '404'; title = 'Error'; - description = expectedErrorMessages.includes(errorMessage) - ? errorMessage - : miscError; + description = isNotFoundError + ? "This playlist hasn't been archived yet." + : "Failed to load playlist's archive entry."; - Astro.response.status = description === miscError ? 500 : 404; - Astro.response.statusText = errorMessage; + Astro.response.status = isNotFoundError ? 404 : 500; + Astro.response.statusText = description; } return { playlist, title, description };