Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/web/src/needs-converge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createFetchStockHub,
forkSubThread,
StockHubCapabilityError,
StockHubRequestError,
type HubSnapshot,
type MailMessage,
type StockHub,
Expand Down Expand Up @@ -701,6 +702,36 @@ describe("stock-only fetch hub", () => {
}) as typeof fetch;
}

function stubFetchRejecting(body: unknown, status: number): typeof fetch {
return (async () => new Response(JSON.stringify(body), { status })) as unknown as typeof fetch;
}

test("a failed request carries the hub's error code and message", async () => {
const hub = createFetchStockHub(
stubFetchRejecting({ error: { code: "invalid_workflow", message: "entry threw" } }, 409),
);

const error = await hub
.deployWorkflow("tnt_1", {
source: {
kind: "asset",
assetId: "ast_1",
package: { format: "source", commitSha: "abc" },
},
entry: "./workflow.js",
sourceOfferingIds: ["off_1"],
defaultSourceOfferingId: "off_1",
})
.then(
() => null,
(cause: unknown) => cause,
);
expect(error).toBeInstanceOf(StockHubRequestError);
expect((error as StockHubRequestError).message).toBe(
"Stock request deployWorkflow failed with HTTP 409. invalid_workflow: entry threw",
);
});

test("mailbox search and thread reads stay typed upstream gaps", async () => {
const hub = createFetchStockHub(stubFetch([], {}));

Expand Down
11 changes: 10 additions & 1 deletion apps/web/src/needs-converge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,21 @@ async function resolveRoleId(
return found.id;
}

const HubErrorEnvelope = type({
error: { code: "string", "message?": "string", "userMessage?": "string" },
});

async function readJson(response: Response, operation: string): Promise<unknown> {
if (!response.ok) {
const envelope = HubErrorEnvelope(await response.json().catch(() => undefined));
const reason =
envelope instanceof type.errors
? ""
: ` ${envelope.error.code}: ${envelope.error.userMessage ?? envelope.error.message ?? ""}`;
throw new StockHubRequestError(
operation,
response.status,
`Stock request ${operation} failed with HTTP ${response.status}.`,
`Stock request ${operation} failed with HTTP ${response.status}.${reason}`,
);
}
return (await response.json()) as unknown;
Expand Down
Loading