fix(http): classify V2 gateway 5xx/408/425 bodies as server failures (Q-6.3-3b)
V2 checked the content type and decoded the body before looking at the status. An nginx 502 with an HTML page became CONTENT_TYPE_MISMATCH and an empty or broken 503/504 JSON body became MALFORMED_JSON: both non-retryable and shown as a contract breach. For a 5xx/408/425 status these branches, and the invalid-envelope branch, now return SERVER_FAILURE with the httpStatus, the same R-3.3 rule V3 uses. Retry follows V2's existing SERVER_FAILURE rule in retry-policy.ts, so 502/503/504 retry and 408/425/500 do not; SERVER_FAILURE was already in retryKinds, so retry-policy.ts is unchanged. The existing "non-JSON response" test used a 502 and pinned the old CONTENT_TYPE_MISMATCH classification; it now uses a 200, which still covers the non-JSON rejection and body redaction. Verified: four new http-client cases (502 HTML, 503 empty JSON, 504 broken JSON, 408 text) failed RED with CONTENT_TYPE_MISMATCH / MALFORMED_JSON and now pass with the expected fetch counts. All lane gates pass except the known removal-fixture test; test:reference-feature timed out once under load and passed on rerun. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1d7ca9dcb8
commit
f139b6f7e2
+27
-10
@@ -40,7 +40,7 @@ import type { ClockPort } from "../../application/ports/clock-port.ts";
|
||||
import type { DiagnosticsPort } from "../../application/ports/diagnostics-port.ts";
|
||||
import type { TelemetryPort } from "../../application/ports/telemetry-port.ts";
|
||||
import type { ApiOperation } from "../../contracts/api-operations.ts";
|
||||
import type { ApiFailure } from "../../contracts/errors.ts";
|
||||
import type { ApiFailure, FailureKind } from "../../contracts/errors.ts";
|
||||
import type { OperationRequestInput } from "./request-builder.ts";
|
||||
import { readBoundedJson } from "./bounded-json.ts";
|
||||
import { admitCredentialHeaders } from "./http-contract-bridge.ts";
|
||||
@@ -841,6 +841,17 @@ export function createHttpClient(
|
||||
|
||||
class LogicalDeadlineError extends Error {}
|
||||
|
||||
/**
|
||||
* Q-6.3-3b. A gateway 5xx/408/425 with an HTML, empty or broken body is a
|
||||
* server outage, not a contract breach: it keeps its status and the normal
|
||||
* SERVER_FAILURE retry rule instead of a non-retryable body error.
|
||||
*/
|
||||
function gatewayFailureOr(status: number, kind: FailureKind): FailureKind {
|
||||
return status >= 500 || status === 408 || status === 425
|
||||
? "SERVER_FAILURE"
|
||||
: kind;
|
||||
}
|
||||
|
||||
async function parseResponse(
|
||||
response: Response,
|
||||
operation: ApiOperation,
|
||||
@@ -861,10 +872,12 @@ async function parseResponse(
|
||||
void response.body?.cancel().catch(() => {});
|
||||
return {
|
||||
ok: false,
|
||||
error: failure("CONTENT_TYPE_MISMATCH", operation.operationId, attempt, {
|
||||
code: "CONTENT_TYPE_MISMATCH",
|
||||
httpStatus: response.status,
|
||||
}),
|
||||
error: failure(
|
||||
gatewayFailureOr(response.status, "CONTENT_TYPE_MISMATCH"),
|
||||
operation.operationId,
|
||||
attempt,
|
||||
{ code: "CONTENT_TYPE_MISMATCH", httpStatus: response.status },
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -875,10 +888,12 @@ async function parseResponse(
|
||||
if (!decoded.ok) {
|
||||
return {
|
||||
ok: false,
|
||||
error: failure(decoded.code, operation.operationId, attempt, {
|
||||
code: decoded.code,
|
||||
httpStatus: response.status,
|
||||
}),
|
||||
error: failure(
|
||||
gatewayFailureOr(response.status, decoded.code),
|
||||
operation.operationId,
|
||||
attempt,
|
||||
{ code: decoded.code, httpStatus: response.status },
|
||||
),
|
||||
};
|
||||
}
|
||||
const envelope = decoded.value;
|
||||
@@ -888,7 +903,9 @@ async function parseResponse(
|
||||
return {
|
||||
ok: false,
|
||||
error: failure(
|
||||
response.ok ? "ENVELOPE_MISMATCH" : statusKind(response.status),
|
||||
response.ok
|
||||
? "ENVELOPE_MISMATCH"
|
||||
: gatewayFailureOr(response.status, statusKind(response.status)),
|
||||
operation.operationId,
|
||||
attempt,
|
||||
{
|
||||
|
||||
@@ -145,7 +145,7 @@ describe("shared HTTP client", () => {
|
||||
server.use(
|
||||
http.get(
|
||||
"https://api.test/api/entities",
|
||||
() => new HttpResponse("<secret>raw body</secret>", { status: 502 }),
|
||||
() => new HttpResponse("<secret>raw body</secret>", { status: 200 }),
|
||||
),
|
||||
);
|
||||
const client = testClient({ baseUrl: "https://api.test", clock });
|
||||
@@ -158,6 +158,43 @@ describe("shared HTTP client", () => {
|
||||
expect(JSON.stringify(result)).not.toContain("raw body");
|
||||
});
|
||||
|
||||
// Q-6.3-3b. A gateway 5xx/408/425 with an HTML, empty or broken body is a
|
||||
// server outage, not a contract breach: it keeps its status and the V2
|
||||
// SERVER_FAILURE retry rule (502/503/504 only).
|
||||
it.each([
|
||||
{ status: 502, contentType: "text/html", body: "<secret>raw body</secret>", fetches: 3 },
|
||||
{ status: 503, contentType: "application/json", body: "", fetches: 3 },
|
||||
{ status: 504, contentType: "application/json", body: "{", fetches: 3 },
|
||||
{ status: 408, contentType: "text/plain", body: "timeout", fetches: 1 },
|
||||
])(
|
||||
"keeps a $status gateway body as a server failure with its status",
|
||||
async ({ status, contentType, body, fetches }) => {
|
||||
let fetched = 0;
|
||||
server.use(
|
||||
http.get("https://api.test/api/entities", () => {
|
||||
fetched += 1;
|
||||
return new HttpResponse(body, {
|
||||
status,
|
||||
headers: { "Content-Type": contentType },
|
||||
});
|
||||
}),
|
||||
);
|
||||
const client = testClient({
|
||||
baseUrl: "https://api.test",
|
||||
clock,
|
||||
random: () => 0,
|
||||
});
|
||||
const result = await client.execute("LIST_ENTITIES");
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "SERVER_FAILURE", httpStatus: status },
|
||||
});
|
||||
expect(fetched).toBe(fetches);
|
||||
expect(JSON.stringify(result)).not.toContain("raw body");
|
||||
},
|
||||
);
|
||||
|
||||
it("classifies malformed JSON and invalid payloads at the boundary", async () => {
|
||||
server.use(
|
||||
http.get(
|
||||
|
||||
Reference in New Issue
Block a user