Public: anyone can read this; the crew can change it.
A wrong link or a removed deal lands on a deal-clock page that says what happened and links back to the clock, not a bare "not found" #72
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/69-a-wrong-link-or-a-removed-deal-lands-on → main 1 commit fork
4 files
changed +81
−5
src/pages/missing.ts added +22
| @@ -0,0 +1,22 @@ | ||
| 1 | /** | |
| 2 | * The page for a path or a deal that is not there: it says so in the | |
| 3 | * product's own frame and points back, to the clock, or to sign-in for a | |
| 4 | * stranger. It is only ever shown for a deal to someone who may read deals, | |
| 5 | * so it tells no one whether an id ever existed. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Who } from "../who.ts"; | |
| 9 | import { layout } from "./layout.ts"; | |
| 10 | ||
| 11 | export function missingPage(input: { who: Who; path: string; deal?: boolean }): string { | |
| 12 | const { who, path, deal = false } = input; | |
| 13 | const sentence = deal | |
| 14 | ? "This deal is not here; it may have been removed." | |
| 15 | : "There is no page at this address."; | |
| 16 | const way = | |
| 17 | who.role === "anon" | |
| 18 | ? `<a class="button" href="/__forge/signin?to=${encodeURIComponent("/")}">Sign in</a>` | |
| 19 | : `<a class="button" href="/">Back to the clock</a>`; | |
| 20 | const body = `<section class="card narrow"><h1>Nothing here</h1><p>${sentence}</p><p>${way}</p></section>`; | |
| 21 | return layout({ body, who, path, heading: "Nothing here", live: false }); | |
| 22 | } |
src/routes.ts +24 −2
| @@ -68,6 +68,7 @@ import { FIND_REFUSAL, find, query } from "./find.ts"; | ||
| 68 | 68 | import { clock } from "./pages/clock.ts"; |
| 69 | 69 | import { dealSheets } from "./pages/sheet.ts"; |
| 70 | 70 | import { dealPage, newDealPage } from "./pages/deal.ts"; |
| 71 | import { missingPage } from "./pages/missing.ts"; | |
| 71 | 72 | import { refusedPage } from "./pages/refused.ts"; |
| 72 | 73 | import { people } from "./people.ts"; |
| 73 | 74 | import { peoplePage } from "./pages/people.ts"; |
| @@ -308,6 +309,22 @@ const STATUS: Record<Failure, number> = { | ||
| 308 | 309 | invalid: 422, |
| 309 | 310 | }; |
| 310 | 311 | |
| 312 | /** A deal that is not there, for a reader who may read deals: the product's own 404. */ | |
| 313 | const missingDeal = (c: Context) => | |
| 314 | html(missingPage({ who: c.who, path: c.url.pathname, deal: true }), 404); | |
| 315 | ||
| 316 | /** Whether a client wants JSON: anything under the API, or an `accept` that asks for it. */ | |
| 317 | const wantsJson = (request: Request, url: URL): boolean => | |
| 318 | url.pathname === "/api" || | |
| 319 | url.pathname.startsWith("/api/") || | |
| 320 | (request.headers.get("accept") ?? "").includes("application/json"); | |
| 321 | ||
| 322 | /** No route matched: JSON for an API client, the product's page for a person. */ | |
| 323 | export function notFound(request: Request, url: URL, who: Who): Response { | |
| 324 | if (wantsJson(request, url)) return json({ error: "not_found" }, 404); | |
| 325 | return html(missingPage({ who, path: url.pathname }), 404); | |
| 326 | } | |
| 327 | ||
| 311 | 328 | /** A result as JSON: the value, or `{ error, message }` with the matching status. */ |
| 312 | 329 | const answer = <T>(result: Result<T>, status = 200): Response => |
| 313 | 330 | result.ok |
| @@ -451,12 +468,14 @@ export const routes: readonly Route[] = [ | ||
| 451 | 468 | path: "/deals/:id", |
| 452 | 469 | handle: (c) => { |
| 453 | 470 | const found = getDeal(c.db, c.who, { id: Number(c.params.id) }); |
| 471 | if (!found.ok && found.error !== "signin_required" && found.error !== "forbidden") | |
| 472 | return missingDeal(c); | |
| 454 | 473 | if (!found.ok) |
| 455 | 474 | return html( |
| 456 | 475 | refusedPage({ |
| 457 | 476 | who: c.who, |
| 458 | 477 | path: c.url.pathname, |
| 459 | heading: found.error === "not_found" || found.error === "invalid" ? "No such deal" : "A deal", | |
| 478 | heading: "A deal", | |
| 460 | 479 | error: found.error, |
| 461 | 480 | message: found.message, |
| 462 | 481 | }), |
| @@ -472,6 +491,7 @@ export const routes: readonly Route[] = [ | ||
| 472 | 491 | // A fragment: every sheet of one deal, fetched by `sheets.js` when the clock asks for one. |
| 473 | 492 | handle: (c) => { |
| 474 | 493 | const found = getDeal(c.db, c.who, { id: Number(c.params.id) }); |
| 494 | if (!found.ok && (found.error === "not_found" || found.error === "invalid")) return missingDeal(c); | |
| 475 | 495 | if (!found.ok) return text(found.message, STATUS[found.error]); |
| 476 | 496 | return html(dealSheets(found.value, c.who, today(), "/")); |
| 477 | 497 | }, |
| @@ -481,12 +501,14 @@ export const routes: readonly Route[] = [ | ||
| 481 | 501 | path: "/deals/:id/timeline", |
| 482 | 502 | handle: (c) => { |
| 483 | 503 | const found = getDeal(c.db, c.who, { id: Number(c.params.id) }); |
| 504 | if (!found.ok && found.error !== "signin_required" && found.error !== "forbidden") | |
| 505 | return missingDeal(c); | |
| 484 | 506 | if (!found.ok) |
| 485 | 507 | return html( |
| 486 | 508 | refusedPage({ |
| 487 | 509 | who: c.who, |
| 488 | 510 | path: c.url.pathname, |
| 489 | heading: found.error === "not_found" || found.error === "invalid" ? "No such deal" : "A deal", | |
| 511 | heading: "A deal", | |
| 490 | 512 | error: found.error, |
| 491 | 513 | message: found.message, |
| 492 | 514 | }), |
src/server.test.ts +33 −1
| @@ -547,7 +547,18 @@ test("a deal's page, its calendar file, and a 404 for one that is not there", as | ||
| 547 | 547 | expect(ics.headers.get("content-disposition")).toMatch(/^attachment; filename="[a-z0-9-]+\.ics"$/); |
| 548 | 548 | expect(ics.headers.get("x-content-type-options")).toBe("nosniff"); |
| 549 | 549 | expect(await ics.text()).toContain("BEGIN:VEVENT"); |
| 550 | expect((await api(`/deals/999999`, {}, alice)).status).toBe(404); | |
| 550 | const gone = await api(`/deals/999999`, {}, alice); | |
| 551 | expect(gone.status).toBe(404); | |
| 552 | expect(gone.headers.get("content-type")).toContain("text/html"); | |
| 553 | expect(gone.headers.get("content-security-policy")).toBeTruthy(); | |
| 554 | const gonePage = await gone.text(); | |
| 555 | expect(gonePage).toContain("Nothing here"); | |
| 556 | expect(gonePage).toContain("This deal is not here; it may have been removed."); | |
| 557 | expect(gonePage).toContain('href="/"'); | |
| 558 | expect((await api(`/deals/999999/sheets`, {}, alice)).status).toBe(404); | |
| 559 | expect(await (await api(`/deals/999999/timeline`, {}, alice)).text()).toContain("Nothing here"); | |
| 560 | // A guest learns nothing: the same sign-in answer for an id that exists and one that does not. | |
| 561 | expect((await fetch(`${base}/deals/999999`)).status).toBe(401); | |
| 551 | 562 | expect((await api(`/deals/999999/ics`, {}, alice)).status).toBe(404); |
| 552 | 563 | const all = await api("/deals/ics", {}, alice); |
| 553 | 564 | expect(all.status).toBe(200); |
| @@ -1238,3 +1249,24 @@ test("a moved date's Date changed notice is sent, declined and undone through th | ||
| 1238 | 1249 | const other = deal.deadlines.find((d) => d.kind === "financing")!; |
| 1239 | 1250 | expect((await tool("send_reminder", { deadline_id: other.id, party_id: buyer.id, moment: "moved" })).isError).toBe(true); |
| 1240 | 1251 | }); |
| 1252 | ||
| 1253 | test("an unknown path is the product's 404 page for people and JSON for API clients", async () => { | |
| 1254 | const lost = await fetch(`${base}/no/such/page`); | |
| 1255 | expect(lost.status).toBe(404); | |
| 1256 | expect(lost.headers.get("content-type")).toContain("text/html"); | |
| 1257 | expect(lost.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 1258 | expect(lost.headers.get("referrer-policy")).toBeTruthy(); | |
| 1259 | expect(lost.headers.get("content-security-policy")).toBeTruthy(); | |
| 1260 | const page = await lost.text(); | |
| 1261 | expect(page).toContain("Nothing here"); | |
| 1262 | expect(page).toContain('class="head"'); | |
| 1263 | expect(page).toContain("/__forge/signin"); | |
| 1264 | const signedIn = await (await api("/no/such/page", {}, alice)).text(); | |
| 1265 | expect(signedIn).toContain('href="/"'); | |
| 1266 | const miss = await fetch(`${base}/api/no/such`); | |
| 1267 | expect(miss.status).toBe(404); | |
| 1268 | expect(await miss.json()).toEqual({ error: "not_found" }); | |
| 1269 | const asked = await fetch(`${base}/elsewhere`, { headers: { accept: "application/json" } }); | |
| 1270 | expect(asked.status).toBe(404); | |
| 1271 | expect(await asked.json()).toEqual({ error: "not_found" }); | |
| 1272 | }); |
src/server.ts +2 −2
| @@ -11,7 +11,7 @@ import { openDb } from "./db.ts"; | ||
| 11 | 11 | import { dataDir, identityKey, title, where } from "./env.ts"; |
| 12 | 12 | import { html, sourceBytes, text } from "./http.ts"; |
| 13 | 13 | import { follow } from "./live.ts"; |
| 14 | import { match } from "./routes.ts"; | |
| 14 | import { match, notFound } from "./routes.ts"; | |
| 15 | 15 | import { door } from "./security.ts"; |
| 16 | 16 | import { who } from "./who.ts"; |
| 17 | 17 | |
| @@ -51,7 +51,7 @@ export function serve(options: ServeOptions) { | ||
| 51 | 51 | request.method === "HEAD" ? "GET" : request.method, |
| 52 | 52 | url.pathname, |
| 53 | 53 | ); |
| 54 | if (found === undefined) return text("not found", 404); | |
| 54 | if (found === undefined) return head(request, notFound(request, url, me)); | |
| 55 | 55 | const response = await found.route.handle({ |
| 56 | 56 | request, |
| 57 | 57 | url, |
new above
offline