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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/deal-clock:mason/69-a-wrong-link-or-a-removed-deal-lands-on → main 1 commit fork

  • Merged. mason/69-a-wrong-link-or-a-removed-deal-lands-on was merged into main as 2e98d2f.

Review

Sign in to review this change.
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";
6868 import { clock } from "./pages/clock.ts";
6969 import { dealSheets } from "./pages/sheet.ts";
7070 import { dealPage, newDealPage } from "./pages/deal.ts";
71 import { missingPage } from "./pages/missing.ts";
7172 import { refusedPage } from "./pages/refused.ts";
7273 import { people } from "./people.ts";
7374 import { peoplePage } from "./pages/people.ts";
@@ -308,6 +309,22 @@ const STATUS: Record<Failure, number> = {
308309 invalid: 422,
309310 };
310311
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
311328 /** A result as JSON: the value, or `{ error, message }` with the matching status. */
312329 const answer = <T>(result: Result<T>, status = 200): Response =>
313330 result.ok
@@ -451,12 +468,14 @@ export const routes: readonly Route[] = [
451468 path: "/deals/:id",
452469 handle: (c) => {
453470 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);
454473 if (!found.ok)
455474 return html(
456475 refusedPage({
457476 who: c.who,
458477 path: c.url.pathname,
459 heading: found.error === "not_found" || found.error === "invalid" ? "No such deal" : "A deal",
478 heading: "A deal",
460479 error: found.error,
461480 message: found.message,
462481 }),
@@ -472,6 +491,7 @@ export const routes: readonly Route[] = [
472491 // A fragment: every sheet of one deal, fetched by `sheets.js` when the clock asks for one.
473492 handle: (c) => {
474493 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);
475495 if (!found.ok) return text(found.message, STATUS[found.error]);
476496 return html(dealSheets(found.value, c.who, today(), "/"));
477497 },
@@ -481,12 +501,14 @@ export const routes: readonly Route[] = [
481501 path: "/deals/:id/timeline",
482502 handle: (c) => {
483503 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);
484506 if (!found.ok)
485507 return html(
486508 refusedPage({
487509 who: c.who,
488510 path: c.url.pathname,
489 heading: found.error === "not_found" || found.error === "invalid" ? "No such deal" : "A deal",
511 heading: "A deal",
490512 error: found.error,
491513 message: found.message,
492514 }),
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
547547 expect(ics.headers.get("content-disposition")).toMatch(/^attachment; filename="[a-z0-9-]+\.ics"$/);
548548 expect(ics.headers.get("x-content-type-options")).toBe("nosniff");
549549 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);
551562 expect((await api(`/deals/999999/ics`, {}, alice)).status).toBe(404);
552563 const all = await api("/deals/ics", {}, alice);
553564 expect(all.status).toBe(200);
@@ -1238,3 +1249,24 @@ test("a moved date's Date changed notice is sent, declined and undone through th
12381249 const other = deal.deadlines.find((d) => d.kind === "financing")!;
12391250 expect((await tool("send_reminder", { deadline_id: other.id, party_id: buyer.id, moment: "moved" })).isError).toBe(true);
12401251 });
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";
1111 import { dataDir, identityKey, title, where } from "./env.ts";
1212 import { html, sourceBytes, text } from "./http.ts";
1313 import { follow } from "./live.ts";
14 import { match } from "./routes.ts";
14 import { match, notFound } from "./routes.ts";
1515 import { door } from "./security.ts";
1616 import { who } from "./who.ts";
1717
@@ -51,7 +51,7 @@ export function serve(options: ServeOptions) {
5151 request.method === "HEAD" ? "GET" : request.method,
5252 url.pathname,
5353 );
54 if (found === undefined) return text("not found", 404);
54 if (found === undefined) return head(request, notFound(request, url, me));
5555 const response = await found.route.handle({
5656 request,
5757 url,

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs