Public: anyone can read this; the crew can change it.
Typed errors at one boundary: name the error kinds src/http/respond.ts can answer and have handlers return them as plain data #16775
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14234-typed-errors-at-one-boundary-name-the-er → main 0 commits fork 3 threads · 3 unresolved
7 files
changed +111
−14
docs/ARCHITECTURE.md +6
| @@ -260,6 +260,12 @@ Two lessons Forgejo is still paying for, designed in up front: | ||
| 260 | 260 | inert `default-src 'none'` CSP on every non-document one — JSON, CSS, |
| 261 | 261 | redirects. A page keeps the nonce'd policy it writes for itself, and an |
| 262 | 262 | app's proxied response is carried untouched: its headers are its own. |
| 263 | - **Typed errors at one boundary**: `http/error_kinds.ts` names the kinds an | |
| 264 | answer can carry (code, status). A handler returns | |
| 265 | `refuse("code", message)`, plain data, and `answer()` in `respond.ts` renders | |
| 266 | the one `{ error, message, hint }` body. To add a kind, add a row to `KINDS` | |
| 267 | and return it; `test/error-kinds.test.ts` holds the table. The offers route | |
| 268 | is moved over as the pilot; other routes move as they are touched. | |
| 263 | 269 | - **Mobile-first**: layouts compose from a single column up; touch targets |
| 264 | 270 | ≥44px; the fast paths (open issue, comment, review a diff, merge) are |
| 265 | 271 | designed on a phone frame first and enhanced for desktop. |
src/actions/offer.ts +2 −1
| @@ -104,7 +104,8 @@ export const OFFERS_WAITING_MAX = 20; | ||
| 104 | 104 | const BRANCH_PREFIX = "contrib/offer"; |
| 105 | 105 | |
| 106 | 106 | export type OfferError = |
| 107 | | { readonly error: "not_found" | "queue_full" | "unavailable" | OfferRefusal } | |
| 107 | | { readonly error: "not_found" } | |
| 108 | | { readonly error: "queue_full" | "unavailable" | OfferRefusal } | |
| 108 | 109 | | { readonly error: "rate_limited"; readonly retryAfter: number }; |
| 109 | 110 | |
| 110 | 111 | export interface OfferInput { |
src/http/error_kinds.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | /** | |
| 2 | * The error kinds an API answer can name (#14234): one table of code and status, | |
| 3 | * and plain data a handler returns in place of building a | |
| 4 | * Response. `answer` in `respond.ts` turns a `Refusal` into the one | |
| 5 | * `{ error, message, hint }` body, so a code and its status are written once. | |
| 6 | * | |
| 7 | * To add a kind: add a row to `KINDS` (code is the key; status is the HTTP | |
| 8 | * status; `wait` for a 429/503 that says how long), then return `refuse("code", | |
| 9 | * message)` from the handler. `test/error-kinds.test.ts` holds the table to | |
| 10 | * the answer. The offers route is the pilot; other routes move over as they | |
| 11 | * are touched. | |
| 12 | */ | |
| 13 | ||
| 14 | export type Kind = | |
| 15 | | { readonly status: number; readonly wait?: undefined } | |
| 16 | /** A 429/503 always says how long: the seconds to ask when the refusal does not. */ | |
| 17 | | { readonly status: 429 | 503; readonly wait: number }; | |
| 18 | ||
| 19 | /** The offers family: the patch refusals are 422, the pressure ones 429/503. */ | |
| 20 | export const KINDS = { | |
| 21 | empty_title: { status: 422 }, | |
| 22 | title_too_long: { status: 422 }, | |
| 23 | body_too_long: { status: 422 }, | |
| 24 | patch_missing: { status: 422 }, | |
| 25 | patch_too_large: { status: 422 }, | |
| 26 | patch_not_text: { status: 422 }, | |
| 27 | patch_touches_git: { status: 422 }, | |
| 28 | patch_bad_path: { status: 422 }, | |
| 29 | patch_does_not_apply: { status: 422 }, | |
| 30 | patch_unsupported: { status: 422 }, | |
| 31 | rate_limited: { status: 429, wait: 60 }, | |
| 32 | queue_full: { status: 429, wait: 60 }, | |
| 33 | unavailable: { status: 503, wait: 60 }, | |
| 34 | } as const satisfies Record<string, Kind>; | |
| 35 | ||
| 36 | export type KindCode = keyof typeof KINDS; | |
| 37 | ||
| 38 | /** A refusal as plain data: what a handler returns, and `answer` renders. */ | |
| 39 | export interface Refusal { | |
| 40 | readonly kind: KindCode; | |
| 41 | readonly message: string; | |
| 42 | readonly retryAfter?: number; | |
| 43 | } | |
| 44 | ||
| 45 | export const refuse = (kind: KindCode, message: string, retryAfter?: number): Refusal => | |
| 46 | retryAfter === undefined ? { kind, message } : { kind, message, retryAfter }; |
src/http/offer_routes.ts +19 −13
| @@ -10,7 +10,7 @@ import { | ||
| 10 | 10 | OFFER_REQUEST_MAX, |
| 11 | 11 | OFFER_RECEIVED, |
| 12 | 12 | OFFER_WORDS, |
| 13 | type OfferRefusal, | |
| 13 | type OfferError, | |
| 14 | 14 | } from "../actions/offer.ts"; |
| 15 | 15 | import { readOutsiderStatus, statusLinks, type OutsiderStatus } from "../core/outsider_status.ts"; |
| 16 | 16 | import { readReceipt } from "../core/offer_receipt.ts"; |
| @@ -21,19 +21,34 @@ import { RateLimiter } from "../core/ratelimit.ts"; | ||
| 21 | 21 | import type { App } from "../app.ts"; |
| 22 | 22 | import type { Handler } from "./context.ts"; |
| 23 | 23 | import { clientBucket } from "./peer.ts"; |
| 24 | import { apiError, boundedBody, json, notFound, securityFloor, str as bodyStr, tryAgain } from "./respond.ts"; | |
| 24 | import { answer, boundedBody, json, notFound, securityFloor, str as bodyStr, tryAgain } from "./respond.ts"; | |
| 25 | import { refuse, type Refusal } from "./error_kinds.ts"; | |
| 25 | 26 | import { PATCH_TEXT } from "./parseable_request.ts"; |
| 26 | 27 | |
| 27 | 28 | /** A full queue drains as people read offers; no clock says when, so a minute. */ |
| 28 | 29 | const QUEUE_RETRY = 60; |
| 29 | 30 | |
| 31 | /** An offer's refusal as a named one: the words are the action's, the status the kind's. */ | |
| 32 | function offerRefusal(refusal: Exclude<OfferError, { error: "not_found" }>): Refusal { | |
| 33 | switch (refusal.error) { | |
| 34 | case "rate_limited": | |
| 35 | return refuse("rate_limited", "too many offers; wait and send again", refusal.retryAfter); | |
| 36 | case "queue_full": | |
| 37 | return refuse("queue_full", "this repository has many offers waiting; try again later", QUEUE_RETRY); | |
| 38 | case "unavailable": | |
| 39 | return refuse("unavailable", "offers cannot be taken just now; try again later", QUEUE_RETRY); | |
| 40 | default: | |
| 41 | return refuse(refusal.error, `${OFFER_WORDS[refusal.error]}; ${OFFER_HINT}`); | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 30 | 45 | /** The handler of `POST /api/v1/repos/:owner/:name/offers`, registered in `api.ts`. */ |
| 31 | 46 | export const offerHandler: Handler = async (ctx) => { |
| 32 | 47 | const found = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!); |
| 33 | 48 | const repo = found && canRead(ctx.app.db, found, ctx.auth.user) ? found : undefined; |
| 34 | 49 | if (!repo) return notFound(); |
| 35 | 50 | const input = await boundedBody(ctx.request, OFFER_REQUEST_MAX, PATCH_TEXT); |
| 36 | if (input === "too_large") return apiError("patch_too_large", 422, OFFER_WORDS.patch_too_large); | |
| 51 | if (input === "too_large") return answer(refuse("patch_too_large", OFFER_WORDS.patch_too_large)); | |
| 37 | 52 | const filed = await fileOffer( |
| 38 | 53 | ctx.app, |
| 39 | 54 | repo, |
| @@ -41,16 +56,7 @@ export const offerHandler: Handler = async (ctx) => { | ||
| 41 | 56 | clientBucket(ctx.request), |
| 42 | 57 | ); |
| 43 | 58 | if (!filed.ok) { |
| 44 | const refusal = filed.error; | |
| 45 | if (refusal.error === "not_found") return notFound(); | |
| 46 | if (refusal.error === "rate_limited") | |
| 47 | return tryAgain("rate_limited", 429, "too many offers; wait and send again", refusal.retryAfter); | |
| 48 | if (refusal.error === "queue_full") | |
| 49 | return tryAgain("queue_full", 429, "this repository has many offers waiting; try again later", QUEUE_RETRY); | |
| 50 | if (refusal.error === "unavailable") | |
| 51 | return tryAgain("unavailable", 503, "offers cannot be taken just now; try again later", QUEUE_RETRY); | |
| 52 | const why: OfferRefusal = refusal.error; | |
| 53 | return json({ error: why, message: `${OFFER_WORDS[why]}; ${OFFER_HINT}` }, 422); | |
| 59 | return filed.error.error === "not_found" ? notFound() : answer(offerRefusal(filed.error)); | |
| 54 | 60 | } |
| 55 | 61 | const links = statusLinks(ctx.url.origin, filed.value.statusToken); |
| 56 | 62 | return json( |
src/http/respond.ts +8
| @@ -14,6 +14,7 @@ import { GUEST_REFUSALS, guestRefusalStatus, isGuestRefusal } from "../core/cont | ||
| 14 | 14 | import { NOT_A_MEMBER } from "../core/roles.ts"; |
| 15 | 15 | import { NEW_ACCOUNT_GUARD_MESSAGE } from "../core/capacity.ts"; |
| 16 | 16 | import { VERIFY_FIRST } from "../actions/verify_words.ts"; |
| 17 | import { KINDS, type Kind, type Refusal } from "./error_kinds.ts"; | |
| 17 | 18 | import type { User } from "../core/users.ts"; |
| 18 | 19 | import { crossOrigin, type Ctx } from "./context.ts"; |
| 19 | 20 | |
| @@ -214,6 +215,13 @@ export const apiError = ( | ||
| 214 | 215 | hint?: string, |
| 215 | 216 | ): Response => json(hint === undefined ? { error: code, message } : { error: code, message, hint }, status); |
| 216 | 217 | |
| 218 | /** A named refusal (`error_kinds.ts`) as the one `{ error, message, hint }` answer. */ | |
| 219 | export function answer(r: Refusal): Response { | |
| 220 | const k: Kind = KINDS[r.kind]; | |
| 221 | if (k.wait !== undefined) return tryAgain(r.kind, k.status, r.message, r.retryAfter ?? k.wait); | |
| 222 | return apiError(r.kind, k.status, r.message); | |
| 223 | } | |
| 224 | ||
| 217 | 225 | /** The API's own words for a resource that is missing or not visible to you. */ |
| 218 | 226 | export const NOT_FOUND_WORDS = "no such resource here, or it is not visible to you"; |
| 219 | 227 | /** The API's own words for a request that carries no credential. */ |
test/error-kinds.test.ts added +26
| @@ -0,0 +1,26 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { KINDS, refuse, type KindCode } from "../src/http/error_kinds.ts"; | |
| 3 | import { answer } from "../src/http/respond.ts"; | |
| 4 | ||
| 5 | test("every kind answers {error, message, hint} with its own status", async () => { | |
| 6 | for (const code of Object.keys(KINDS) as KindCode[]) { | |
| 7 | const res = answer(refuse(code, "words")); | |
| 8 | const body = (await res.json()) as Record<string, unknown>; | |
| 9 | expect(res.status).toBe(KINDS[code].status); | |
| 10 | expect(body.error).toBe(code); | |
| 11 | expect(body.message).toBe("words"); | |
| 12 | } | |
| 13 | }); | |
| 14 | ||
| 15 | test("a pressure kind says how long: the refusal's own, else the row's", async () => { | |
| 16 | const res = answer(refuse("queue_full", "busy", 12)); | |
| 17 | expect(res.status).toBe(429); | |
| 18 | expect(res.headers.get("retry-after")).toBe("12"); | |
| 19 | expect(((await res.json()) as { retryAfter: number }).retryAfter).toBe(12); | |
| 20 | const dflt = answer(refuse("unavailable", "later")); | |
| 21 | expect(dflt.headers.get("retry-after")).toBe("60"); | |
| 22 | }); | |
| 23 | ||
| 24 | test("refuse is plain data", () => { | |
| 25 | expect(refuse("empty_title", "x")).toEqual({ kind: "empty_title", message: "x" }); | |
| 26 | }); |
test/retry-after.test.ts +4
| @@ -32,6 +32,10 @@ const PERF_LINE = `src/http/server.ts: ${readFileSync("src/http/server.ts", "utf | ||
| 32 | 32 | const EXEMPT: Record<string, string> = { |
| 33 | 33 | "src/http/api/user.ts: return json({ ...said, ...blockedFields(noMailBlocked(ctx.app, ctx.auth.user!)) }, 503, Object.fromEntries(refused.headers));": |
| 34 | 34 | "the headers are tryAgain's own, spread from refused.headers", |
| 35 | "src/http/error_kinds.ts: rate_limited: { status: 429, wait: 60 },": "a kind's row; answer() renders it through tryAgain", | |
| 36 | "src/http/error_kinds.ts: queue_full: { status: 429, wait: 60 },": "a kind's row; answer() renders it through tryAgain", | |
| 37 | "src/http/error_kinds.ts: unavailable: { status: 503, wait: 60 },": "a kind's row; answer() renders it through tryAgain", | |
| 38 | "src/http/error_kinds.ts: | { readonly status: 429 | 503; readonly wait: number };": "the type of a kind that waits; answer() renders it through tryAgain", | |
| 35 | 39 | [PERF_LINE]: "a comparison, not a response", |
| 36 | 40 | "src/http/respond.ts: if (status === 429 || status === 503) {": "json()'s floor itself", |
| 37 | 41 | "src/http/respond.ts: status: 429 | 503,": "tryAgain's own signature", |
new above
offline
No row sets
hint; remove the field and itsanswer()branch, or use it for a real kind and test it.