fix: give each API surface its own error-code enum
The public site answered every screen with the terminal error surface. Three defects stacked, and each one hid the next. The first refused the request outright: `attachCredentials` asks the Studio helper, which returns null for a profile it does not own, and the fallback below read the session and rejected anything not authenticated. Public reads declare the ANONYMOUS profile, so a signed-out visitor — the public site's entire audience — never got a request out of the browser. An anonymous profile carries no credentials by definition and must never consult the session. With requests flowing, the second surfaced: `envelopeError()` pinned `ApiError.code` to the Studio enum and all three surfaces shared it. Public and Management each declare their own enum in their own contract, so every error they returned failed validation and arrived as a CONTRACT_VIOLATION — an unclassifiable transport fault — rather than the domain error it was. A strict enum checked against the wrong surface's contract still looks strict, which is why no gate caught it. Each surface now passes its own contract's codes. The third was the not-found path: it read `status` and `code` off the problem body, but the envelope has no `status` and names the code for its surface (PUBLIC_RESOURCE_NOT_FOUND, not NOT_FOUND). The HTTP status from the transport is the authoritative signal and the only one that holds across both shapes. The regression test composes the real runtime adapters against the deployed backend's actual 404 body. Neither the gateway tests (which stub the executor) nor the screen tests (which stub the gateway) cover this seam, and the whole outage lived in it. Two page-level fixes came out of the same investigation: the profile page asked for two project slugs that only ever existed in the static fixture, and the index pages held their fixed header copy behind a request that had nothing to do with it. Headers now paint immediately; only the sections that are actually waiting show a fallback, and an empty list says so instead of rendering blank.
This commit is contained in:
@@ -38,8 +38,19 @@ export function createHttpManagementGateway(
|
||||
const outcome = await deps.operations.execute(operationId, input, { routeId: ROUTE_ID });
|
||||
if (outcome.kind === "SUCCESS") return outcome.value as T;
|
||||
if (outcome.kind === "PROBLEM") {
|
||||
const problem = outcome.problem as Readonly<{ code?: string }> | null;
|
||||
throw new ManagementGatewayError(operationId, problem?.code ?? "PROBLEM");
|
||||
// The management surface answers with the ADR-006 envelope, which nests
|
||||
// the code under `error` — reading `problem.code` found nothing and every
|
||||
// failure surfaced as the literal "PROBLEM", matching no i18n key.
|
||||
const body = outcome.problem as
|
||||
| Readonly<{ code?: unknown; error?: Readonly<{ code?: unknown }> }>
|
||||
| null;
|
||||
const code =
|
||||
typeof body?.code === "string"
|
||||
? body.code
|
||||
: typeof body?.error?.code === "string"
|
||||
? body.error.code
|
||||
: "PROBLEM";
|
||||
throw new ManagementGatewayError(operationId, code);
|
||||
}
|
||||
throw new ManagementGatewayError(operationId, outcome.kind);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,20 @@ function gatewayError(operationId: string, detail: string): PublicContentGateway
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the backend's error code out of either response shape — RFC7807 puts it
|
||||
* at the top level, the ADR-006 envelope nests it under `error`. Without this
|
||||
* every failure was reported as the literal "PROBLEM", which told a reader
|
||||
* nothing and matched no i18n key.
|
||||
*/
|
||||
function problemCode(problem: unknown): string {
|
||||
if (!problem || typeof problem !== "object") return "PROBLEM";
|
||||
const body = problem as Readonly<{ code?: unknown; error?: Readonly<{ code?: unknown }> }>;
|
||||
if (typeof body.code === "string") return body.code;
|
||||
if (typeof body.error?.code === "string") return body.error.code;
|
||||
return "PROBLEM";
|
||||
}
|
||||
|
||||
export function createHttpPublicContentGateway(
|
||||
deps: Readonly<{ operations: StudioOperationExecutor }>,
|
||||
): PublicContentQueries {
|
||||
@@ -53,9 +67,15 @@ export function createHttpPublicContentGateway(
|
||||
const outcome = await deps.operations.execute(operationId, input, { routeId: ROUTE_ID });
|
||||
if (outcome.kind === "SUCCESS") return outcome.value as T;
|
||||
if (outcome.kind === "PROBLEM") {
|
||||
const problem = outcome.problem as Readonly<{ status?: number; code?: string }> | null;
|
||||
if (problem?.status === 404 || problem?.code === "NOT_FOUND") return NOT_FOUND;
|
||||
throw gatewayError(operationId, problem?.code ?? "PROBLEM");
|
||||
// The HTTP status is the authoritative signal, and the only one that
|
||||
// holds across both shapes this surface answers with. RFC7807 carries
|
||||
// `status` in the body; the ADR-006 envelope does not — it puts the
|
||||
// reason in `error.category` and a backend-specific string in
|
||||
// `error.code` (PUBLIC_RESOURCE_NOT_FOUND, not NOT_FOUND). The old
|
||||
// body-only check matched neither, so every 404 raised the terminal
|
||||
// error surface on a page whose real state was "this does not exist".
|
||||
if (outcome.metadata.status === 404) return NOT_FOUND;
|
||||
throw gatewayError(operationId, problemCode(outcome.problem));
|
||||
}
|
||||
throw gatewayError(operationId, outcome.kind);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user