Public: anyone can read this; the crew can change it.
A page that failed to render shows what failed, a Retry link to the same address and the request id on one line #16061
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16045-a-page-that-failed-to-render-shows-what → main 0 commits fork 5 threads · 5 unresolved
src/web/failed_page.ts +9 −4
| @@ -45,10 +45,15 @@ export function parentOf(pathname: string): string { | ||
| 45 | 45 | return segments.length <= 1 ? "/" : `/${segments.slice(0, -1).join("/")}`; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | /** Where Try again goes: this path for a read, home for anything that wrote. */ | |
| 48 | /** | |
| 49 | * Where Retry goes: the same address for a read — this path and the request's | |
| 50 | * query — and home for anything that wrote. A path that starts `//` would | |
| 51 | * leave the origin, so it goes home too. | |
| 52 | */ | |
| 49 | 53 | export function retryTarget(request: Request, pathname: string): string { |
| 50 | 54 | const read = request.method === "GET" || request.method === "HEAD"; |
| 51 | return read && !pathname.startsWith("//") ? pathname : "/"; | |
| 55 | if (!read || !pathname.startsWith("/") || pathname.startsWith("//")) return "/"; | |
| 56 | return pathname + new URL(request.url).search; | |
| 52 | 57 | } |
| 53 | 58 | |
| 54 | 59 | export function failedPage(request: Request, pathname: string, id: string): Response { |
| @@ -59,9 +64,9 @@ export function failedPage(request: Request, pathname: string, id: string): Resp | ||
| 59 | 64 | ${crumbs.length === 0 ? null : crumbNav(crumbs, { ariaLabel: "Location" })} |
| 60 | 65 | </header> |
| 61 | 66 | <main class="shell" id="main"> |
| 62 | <div class="page-head"><h1>Something broke on our side. Reference: <code>${id}</code></h1></div> | |
| 67 | <div class="page-head"><h1>Could not render <code>${pathname.length > 120 ? `${pathname.slice(0, 120)}…` : pathname}</code>. Reference: <code>${id}</code></h1></div> | |
| 63 | 68 | <p class="muted">This is the forge's fault, not yours. <a href="/tell">Tell the crew</a> and quote the reference.</p> |
| 64 | <p class="actions">${btn("Try again", { href: retryTarget(request, pathname), variant: "primary" })} ${btn("Go home", { href: "/" })}</p> | |
| 69 | <p class="actions">${btn("Retry", { href: retryTarget(request, pathname), variant: "primary" })} ${btn("Go home", { href: "/" })}</p> | |
| 65 | 70 | </main>`; |
| 66 | 71 | const text = `<!doctype html><html lang="en"><head><meta charset="utf-8" /> |
| 67 | 72 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
test/error-page-500.test.ts +7 −2
| @@ -54,7 +54,7 @@ test("a throwing page answers 500 with chrome, the way back and an id found in t | ||
| 54 | 54 | expect(body).toContain('class="topbar"'); |
| 55 | 55 | expect(body).toContain('class="crumbs"'); |
| 56 | 56 | expect(body).toContain('href="/ada/notes"'); // the parent |
| 57 | expect(body).toContain("Try again"); | |
| 57 | expect(body).toContain("Retry"); | |
| 58 | 58 | expect(body).toContain("the forge's fault"); |
| 59 | 59 | expect(body).toContain('href="/tell"'); |
| 60 | 60 | expect(body).toContain('href="/"'); |
| @@ -103,9 +103,14 @@ test("crumbs and parent come from the path alone", () => { | ||
| 103 | 103 | expect(parentOf("/a")).toBe("/"); |
| 104 | 104 | }); |
| 105 | 105 | |
| 106 | test("Try again re-requests a read, and sends anything that wrote home", () => { | |
| 106 | test("Retry re-requests a read, and sends anything that wrote home", () => { | |
| 107 | 107 | const req = (method: string) => new Request("http://x/a/b", { method }); |
| 108 | 108 | expect(retryTarget(req("GET"), "/a/b")).toBe("/a/b"); |
| 109 | 109 | expect(retryTarget(req("POST"), "/a/b")).toBe("/"); |
| 110 | 110 | expect(retryTarget(req("GET"), "//evil.example")).toBe("/"); |
| 111 | const query = (method: string) => new Request("http://x/ada/notes?tab=open&q=a%20b", { method }); | |
| 112 | expect(retryTarget(query("GET"), "/ada/notes")).toBe("/ada/notes?tab=open&q=a%20b"); | |
| 113 | expect(retryTarget(query("HEAD"), "/ada/notes")).toBe("/ada/notes?tab=open&q=a%20b"); | |
| 114 | expect(retryTarget(query("POST"), "/ada/notes")).toBe("/"); | |
| 115 | expect(retryTarget(query("GET"), "//evil.example")).toBe("/"); | |
| 111 | 116 | }); |
test/error-page.test.ts +2 −2
| @@ -50,10 +50,10 @@ async function everyFamilyFails(install: () => () => void): Promise<void> { | ||
| 50 | 50 | const body = await res.text(); |
| 51 | 51 | expect(res.status, path).toBe(500); |
| 52 | 52 | const logs = spy.mock.calls.map((c) => c.join(" ")).join("\n"); |
| 53 | const id = /<code>([0-9a-f]{8})<\/code>/.exec(body)?.[1]; | |
| 53 | const id = /Reference: <code>([0-9a-f]{8})<\/code>/.exec(body)?.[1]; | |
| 54 | 54 | expect(id, path).toBeDefined(); |
| 55 | 55 | expect(logs, path).toContain(id!); |
| 56 | expect(body, path).toContain("Something broke on our side. Reference:"); | |
| 56 | expect(body, path).toContain("Could not render"); | |
| 57 | 57 | expect(body, path).not.toContain(FAULT); |
| 58 | 58 | expect(body, path).not.toContain(dataDir); |
| 59 | 59 | expect(body, path).not.toMatch(/\.tsx?\b/); |
test/error-reference.test.ts +1 −1
| @@ -43,7 +43,7 @@ const leaks = (b: string) => { | ||
| 43 | 43 | test("the page says one line with the reference, the log has the same id", async () => { |
| 44 | 44 | const { res, body, logs } = await failing("/ada/notes/issues", "text/html"); |
| 45 | 45 | expect(res.status).toBe(500); |
| 46 | const ref = /Something broke on our side\. Reference: <code>([0-9a-f]{8})<\/code>/.exec(body)?.[1]; | |
| 46 | const ref = /Reference: <code>([0-9a-f]{8})<\/code>/.exec(body)?.[1]; | |
| 47 | 47 | expect(ref).toBeDefined(); |
| 48 | 48 | expect(logs).toContain(ref!); |
| 49 | 49 | leaks(body); |
test/failed-page.test.ts added +36
| @@ -0,0 +1,36 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { failedPage } from "../src/web/failed_page.ts"; | |
| 3 | ||
| 4 | test("a failed page names the escaped path once, retries it, and prints the id once", async () => { | |
| 5 | const path = "/a/<script>alert(1)</script>"; | |
| 6 | const res = failedPage(new Request("http://x/a/b"), path, "deadbeef"); | |
| 7 | const body = await res.text(); | |
| 8 | expect(res.status).toBe(500); | |
| 9 | expect(body).toContain("Could not render <code>"); | |
| 10 | expect(body).not.toContain("<script>alert"); | |
| 11 | expect(body).toContain("<script>"); | |
This regex matches the crumb link |
||
| 12 | const retry = body.match(/<a[^>]*href="([^"]*)"[^>]*>\s*Retry</); | |
The Retry href assertion only covers a plain path. Add a case with a query and a POST (which should go home) so the test would fail if the same-address behaviour broke. |
||
| 13 | expect(retry?.[1]).toBe("/a/<script>alert(1)</script>"); | |
| 14 | expect(body.split("deadbeef").length - 1).toBe(1); | |
| 15 | expect(res.headers.get("x-request-id")).toBe("deadbeef"); | |
| 16 | expect(res.headers.get("content-security-policy")).toBeTruthy(); | |
| 17 | }); | |
| 18 | ||
| 19 | const retryOf = (body: string) => body.match(/<a[^>]*href="([^"]*)"[^>]*>\s*Retry</)?.[1]; | |
| 20 | ||
| 21 | test("Retry reloads the same address, query included, and an escaped one", async () => { | |
| 22 | const res = failedPage(new Request("http://x/ada/notes?tab=open&q=%3Cb%3E"), "/ada/notes", "cafef00d"); | |
| 23 | expect(retryOf(await res.text())).toBe("/ada/notes?tab=open&q=%3Cb%3E"); | |
| 24 | }); | |
| 25 | ||
| 26 | test("Retry after a POST goes home, not back to the write", async () => { | |
| 27 | const req = new Request("http://x/ada/notes?tab=open", { method: "POST" }); | |
| 28 | expect(retryOf(await failedPage(req, "/ada/notes", "cafef00d").text())).toBe("/"); | |
| 29 | }); | |
| 30 | ||
| 31 | test("a long unbroken path in the heading wraps instead of widening a phone", async () => { | |
| 32 | const { components } = await import("../src/web/style/components.ts"); | |
| 33 | const body = await failedPage(new Request("http://x/a"), `/${"x".repeat(200)}`, "cafef00d").text(); | |
| 34 | expect(body).toMatch(/<div class="page-head"><h1>Could not render <code>/); | |
| 35 | expect(components).toContain(".page-head h1 { min-width: 0; overflow-wrap: anywhere; }"); | |
| 36 | }); |
Box 1: Retry should reload the same address, query included. Take the request URL's search as well as the pathname, keep the '//' guard, and add a test with a query string.