From 26d5644d8e1584fa0ed55de8708d6ce6fe7b6d9f Mon Sep 17 00:00:00 2001 From: George Zeng <63271460+georgezzeng@users.noreply.github.com> Date: Thu, 30 Jan 2025 12:04:34 -0600 Subject: [PATCH 1/2] enhance error handling --- src/app/page.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 5e98658..164558e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -25,7 +25,6 @@ function MarkdownContent() { const markdownUrl = searchParams.get("url"); const [article, setArticle] = useState(null); - const [error, setError] = useState(undefined); useEffect(() => { @@ -53,9 +52,19 @@ function MarkdownContent() { const markdown = await response.text() const {data, content} = grayMatter(markdown); + if(!data && !content){ + setError("No frontmatter or content found") + return + } const path = markdownUrl.split("/").slice(-1)[0]; + if(!path){ + setError("No path found") + } const date = new Date(path.split("-").slice(0, 3).join("-")); + if(!date){ + setError("No date found") + } const article = { slug: [], @@ -66,7 +75,6 @@ function MarkdownContent() { } setArticle(article as BackendArticle) - setError(undefined) })(); }, [markdownUrl, searchParams]); From b5dbceb85ffac87c659ec13a06d45a4041796782 Mon Sep 17 00:00:00 2001 From: George Zeng <63271460+georgezzeng@users.noreply.github.com> Date: Thu, 30 Jan 2025 12:42:59 -0600 Subject: [PATCH 2/2] enhance error handling --- src/app/page.tsx | 77 +++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 164558e..d760fa0 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -26,6 +26,7 @@ function MarkdownContent() { const [article, setArticle] = useState(null); const [error, setError] = useState(undefined); + const [isLoading, setIsLoading] = useState(true); useEffect(() => { (async () => { @@ -37,44 +38,50 @@ function MarkdownContent() { return; } - let response; try { - response = await fetch(markdownUrl); + const response = await fetch(markdownUrl); if (!response.ok) { setError(`Failed to fetch markdown: ${response.statusText}`); - return + setIsLoading(false); + return; } - } catch (e) { - setError(`Failed to fetch markdown: ${e}`); - return - } - const markdown = await response.text() + const markdown = await response.text(); - const {data, content} = grayMatter(markdown); - if(!data && !content){ - setError("No frontmatter or content found") - return - } + let parsedData; + try { + parsedData = grayMatter(markdown); + } catch { + setError("Error parsing markdown file. Ensure it has valid frontmatter"); + setIsLoading(false); + return; + } - const path = markdownUrl.split("/").slice(-1)[0]; - if(!path){ - setError("No path found") - } - const date = new Date(path.split("-").slice(0, 3).join("-")); - if(!date){ - setError("No date found") - } + const { data, content } = parsedData; + const pathParts = markdownUrl.split("/"); + const filename = pathParts[pathParts.length - 1] || ""; - const article = { - slug: [], - date: date, - path: markdownUrl.split("/").slice(-1)[0], - content, - ...(data as Omit) - } + if (!filename) { + setError("Invalid markdown file path"); + setIsLoading(false); + return; + } - setArticle(article as BackendArticle) + const dateParts = filename.split("-").slice(0, 3); + const date = dateParts.length === 3 ? new Date(dateParts.join("-")) : new Date(); + + setArticle({ + slug: [], + date: date, + path: filename, + content, + ...(data as Omit) + } as BackendArticle); + } catch { + setError("Article fetch failed"); + } finally { + setIsLoading(false); + } })(); }, [markdownUrl, searchParams]); @@ -87,12 +94,20 @@ function MarkdownContent() { ) } + if (isLoading) { + return ( + +

Loading...

+
+ ); + } + if (!article) { return ( -

Loading

+

No article found

- ) + ); } return (