Files
clean-architecture-frontend…/src/adapters/http/platform-error-mapping.ts
T

46 lines
1.6 KiB
TypeScript

import type { AppError } from "../../platform/errors/app-error.ts";
import { PLATFORM_ERRORS } from "../../platform/errors/platform-errors.ts";
import type { HttpExecutionOutcome } from "./http-execution-v3.ts";
/**
* Spec §17. Adapter-boundary normalization of transport-level outcomes into
* the platform error envelope. Only failures no feature owns are mapped here;
* a `PROBLEM`, an auth verdict or a scope fence is interpreted by its owner
* (the feature binding or the session), so those return `null`.
*/
export function platformErrorForOutcome(
outcome: HttpExecutionOutcome<unknown, unknown>,
): AppError | null {
switch (outcome.kind) {
case "CANCELLED":
return PLATFORM_ERRORS.REQUEST_CANCELLED;
case "TRANSPORT_FAILURE":
switch (outcome.failure.kind) {
case "TIMEOUT":
return PLATFORM_ERRORS.REQUEST_TIMEOUT;
case "ABORTED_BY_CALLER":
return PLATFORM_ERRORS.REQUEST_CANCELLED;
case "OVERLOADED_BEFORE_SEND":
return PLATFORM_ERRORS.TRAFFIC_BUDGET_EXCEEDED;
case "ABORTED_BY_SCOPE":
return null;
default:
return PLATFORM_ERRORS.NETWORK_UNAVAILABLE;
}
case "CONTRACT_VIOLATION":
switch (outcome.violation.kind) {
case "RESPONSE_TOO_LARGE":
return PLATFORM_ERRORS.RESPONSE_TOO_LARGE;
case "SCOPE_FENCED":
case "MISSING_IDEMPOTENCY_KEY":
case "UNEXPECTED_IDEMPOTENCY_KEY":
case "FINAL_REQUEST_INVARIANT_FAILED":
return null;
default:
return PLATFORM_ERRORS.RESPONSE_INVALID;
}
default:
return null;
}
}