From 7f36f1c3ef6e5c83c55ce09e2e5ebd4b1b7fdd8e Mon Sep 17 00:00:00 2001 From: Ethan Carter Edwards Date: Mon, 6 Jan 2025 15:13:27 -0500 Subject: [PATCH] better error handling for invalid api response --- pages/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pages/index.js b/pages/index.js index d2dad56..e04ad31 100644 --- a/pages/index.js +++ b/pages/index.js @@ -58,12 +58,15 @@ export async function getServerSideProps() { const posts = getAllPosts(); - await fetch(`https://api.ethancedwards.com/quotes/v1`).then((res) => { - final = res; - }).catch((error) => { - quotes = { quote: "Value your freedom or you will lose it, teaches history", author: "Richard Stallman" }; + // await fetch(`https://api.ethancedwards.com/quotes/v1`).then((res) => { + await fetch(`http://localhost/quotes/v1`).then((res) => { + if (!res.ok) { + final = { quote: "Value your freedom or you will lose it, teaches history", author: "Richard Stallman" }; + } else { + final = res.json(); + } }); - quotes = await final.json(); + quotes = await final; return { props: { quotes, posts } }; }