From 83cf90ba277d94ee837706f01f2cbd50142c22ec Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 12:24:25 +0900 Subject: [PATCH] fix(http): return a mapping failure when a feature mapper throws (R-3.5) The feature binding called mapSuccess/mapProblem without a guard, unlike V2, mapWithBoundaryRegistry and the RPC mapper. A throwing mapper made execute() reject, so a command the server had confirmed (APPLIED_CONFIRMED) was normalized downstream to MAYBE_APPLIED. Both calls are now wrapped; a throw returns MAPPING_CONTRACT_VIOLATION (code MAPPING_CONTRACT_VIOLATION, same as V2) with the outcome's effect and HTTP status. RED before fix: feature-http-binding.test.ts "returns a mapping failure with the server effect when mapSuccess/mapProblem throws" (execute rejected with the mapper TypeError). Gates: check:architecture, check:types:app, check:types:test, lint, test:component, test:integration, test:contract, test:reference-feature pass. test:unit passes except removal-fixture.test.ts (needs generated artifacts/ release evidence absent in a fresh worktree; unrelated). Co-Authored-By: Claude Opus 5.5 --- src/adapters/http/feature-http-binding.ts | 26 +++++++++- .../feature-http-binding.test.ts | 51 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/adapters/http/feature-http-binding.ts b/src/adapters/http/feature-http-binding.ts index c6a2f267..4e083218 100644 --- a/src/adapters/http/feature-http-binding.ts +++ b/src/adapters/http/feature-http-binding.ts @@ -295,9 +295,26 @@ function projectExecutionOutcome( | undefined, outcome: HttpExecutionOutcome, ): Result { + // A throwing feature mapper is a mapping defect, not a transport failure: it + // must not reject and lose the effect the server already confirmed. + const mapperThrew = ( + effect: FailureEffectCertainty, + httpStatus: number, + ): Result => + failure( + "MAPPING_CONTRACT_VIOLATION", + operationId, + "MAPPING_CONTRACT_VIOLATION", + { httpStatus, effect }, + ); switch (outcome.kind) { case "SUCCESS": { - const mapped = mapSuccess(outcome.value); + let mapped: MappingResult; + try { + mapped = mapSuccess(outcome.value); + } catch { + return mapperThrew(outcome.effect, outcome.metadata.status); + } return mapped.ok ? Object.freeze({ ok: true as const, value: mapped.value }) : failure( @@ -308,7 +325,12 @@ function projectExecutionOutcome( ); } case "PROBLEM": { - const mapped = mapProblem?.(outcome.problem, outcome.metadata); + let mapped: FeatureHttpProblemMapping | undefined; + try { + mapped = mapProblem?.(outcome.problem, outcome.metadata); + } catch { + return mapperThrew(outcome.effect, outcome.metadata.status); + } return failure( mapped?.kind ?? kindForStatus(outcome.metadata.status), operationId, diff --git a/tests/contract/reusable-capability/feature-http-binding.test.ts b/tests/contract/reusable-capability/feature-http-binding.test.ts index a2e65f58..a4960a50 100644 --- a/tests/contract/reusable-capability/feature-http-binding.test.ts +++ b/tests/contract/reusable-capability/feature-http-binding.test.ts @@ -219,6 +219,57 @@ describe("feature HTTP binding", () => { }, ); + it.each([ + [ + "mapSuccess", + Object.freeze({ + kind: "SUCCESS" as const, + value: Object.freeze({ id: "resource-1", title: "Reference" }), + metadata: Object.freeze({ status: 201 }), + effect: "APPLIED_CONFIRMED" as const, + }), + ], + [ + "mapProblem", + Object.freeze({ + kind: "PROBLEM" as const, + problem: Object.freeze({ code: "RESOURCE_NAME_EXISTS" }), + metadata: Object.freeze({ status: 409 }), + effect: "APPLIED_CONFIRMED" as const, + }), + ], + ])( + "returns a mapping failure with the server effect when %s throws", + async (_mapper, outcome) => { + const scripted = scriptedExecutor(outcome); + const binding = createFeatureHttpBinding(scripted.executor, { + LOAD_RESOURCE: defineFeatureHttpOperation({ + contract: LOAD_RESOURCE_CONTRACT, + routeId: "RESOURCE_DETAIL", + mapSuccess(): never { + throw new TypeError("mapper defect"); + }, + mapProblem(): never { + throw new TypeError("mapper defect"); + }, + }), + } as const); + + const result = await binding.execute("LOAD_RESOURCE", { + resourceId: "resource-1", + }); + + expect(result).toMatchObject({ + ok: false, + error: { + kind: "MAPPING_CONTRACT_VIOLATION", + code: "MAPPING_CONTRACT_VIOLATION", + effect: "APPLIED_CONFIRMED", + }, + }); + }, + ); + it("lets the feature interpret a typed business problem", async () => { const scripted = scriptedExecutor( Object.freeze({