From ce44679ced087d5f409ea3c339b30c5632ac1c18 Mon Sep 17 00:00:00 2001 From: Mark Suckerberg Date: Mon, 11 Mar 2024 23:17:24 -0500 Subject: [PATCH] More informational error handling --- package.json | 2 +- src/index.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index dbafe20..d74ed2c 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "wrangler": "^3.0.0" }, "dependencies": { - "typeble": "^0.3.1" + "typeble": "^0.5.0" }, "homepage": "https://github.com/MarkSuckerberg/typeble", "bugs": { diff --git a/src/index.ts b/src/index.ts index 5dafa85..04a2537 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import { FetchPost, GetNotes, + TumblrAPIError, TumblrBlocksPost, TumblrNeueAudioBlock, - TumblrNeueAudioBlockBase, TumblrNeueImageBlock, TumblrNeueTextBlock, TumblrNeueVideoBlock, @@ -48,7 +48,9 @@ export default { true ); } catch (error) { - return new Response('Error retrieving post', { status: 404 }); + const tumblrUrl = new URL(`https://www.tumblr.com/${username}/${postID}`); + + return errorPage(error, tumblrUrl); } const originalPost = post.trail[0] as TumblrBlocksPost; @@ -209,3 +211,48 @@ async function mainPage( }, }); } + +async function errorPage(error: unknown, url: URL) { + if (!(error instanceof TumblrAPIError)) { + return new Response('Unknown error fetching post', { status: 500 }); + } + + const errorDescription = + error.response.meta.status === 303 + ? "Login required to view this account's posts" + : error.response.meta.msg; + + const html = ` + + txTumblr + + + + + + + + + + + + + + + + + + + +

Error ${error.response.meta.status}: ${error.response.meta.msg}

+

${error.response.errors?.at(0)?.detail}

+ + `; + + return new Response(html, { + headers: { + 'content-type': 'text/html;charset=UTF-8', + 'status': error.response.meta.status.toString(), + }, + }); +}