From ae000d743ef54368d21596d590e1acc0d90b370e Mon Sep 17 00:00:00 2001 From: Joe Mou Date: Thu, 3 Sep 2026 23:08:08 -0400 Subject: Handle route errors in middleware Every page route wrapped its scrape in tryRender, which awaited the promise, rendered the template on success, and turned a failure into a redirect or an error page. Hono's onError does that mapping for all of them at once, so handlers now await their scrape inline and both wrappers are gone. Fragments under /api/ still fail as an empty body rather than a page; onError checks for them first, since a fragment turns a redirect into a 404 instead of following it. RedirectError becomes an abstract base with InternalRedirectError and ExternalRedirectError under it, so the handler dispatches on the type instead of sniffing the location for an https:// prefix. One behavior change: a thrown value that is not an Error no longer renders error.eta, because Hono only routes Error instances to onError. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01QERwoRJhuqMhKzzCUW4Skv --- src/scraper.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/scraper.ts') diff --git a/src/scraper.ts b/src/scraper.ts index 6461b19..fb48700 100644 --- a/src/scraper.ts +++ b/src/scraper.ts @@ -13,16 +13,20 @@ export class GitHubHTTPError extends Error { } } -export class RedirectError extends Error { +export abstract class RedirectError extends Error { location: string; constructor(location: string) { super(`Redirect to ${location}`); - this.name = "RedirectError"; + this.name = this.constructor.name; this.location = location; } } +export class InternalRedirectError extends RedirectError {} + +export class ExternalRedirectError extends RedirectError {} + interface TreeItem { contentType: "directory" | "file"; name: string; @@ -423,7 +427,7 @@ async function fetchGitHubResponse(path: string, accept: string): Promise { return parseOrganization(document, owner); } // Not a profile page (marketing pages like github.com/features, etc.). - throw new RedirectError(`https://github.com/${owner}`); + throw new ExternalRedirectError(`https://github.com/${owner}`); } // The text directly inside an element, ignoring any nested elements' text. -- cgit v1.3.1